kernel/ixgbe: Add missing braces.
[dragonfly.git] / sys / netbt / hci_link.c
blob8995ff1268bb91baddcf8c7ec742093d029e4475
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 $ */
5 /*-
6 * Copyright (c) 2005 Iain Hibbert.
7 * Copyright (c) 2006 Itronix Inc.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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>
38 #include <sys/mbuf.h>
39 #include <sys/proc.h>
40 #include <sys/queue.h>
41 #include <sys/systm.h>
42 #include <sys/endian.h>
43 #include <sys/callout.h>
44 #include <net/if.h>
45 #include <sys/bus.h>
47 #include <netbt/bluetooth.h>
48 #include <netbt/hci.h>
49 #include <netbt/l2cap.h>
50 #include <netbt/sco.h>
52 /*******************************************************************************
54 * HCI ACL Connections
58 * Automatically expire unused ACL connections after this number of
59 * seconds (if zero, do not expire unused connections) [sysctl]
61 int hci_acl_expiry = 10; /* seconds */
64 * hci_acl_open(unit, bdaddr)
66 * open ACL connection to remote bdaddr. Only one ACL connection is permitted
67 * between any two Bluetooth devices, so we look for an existing one before
68 * trying to start a new one.
70 struct hci_link *
71 hci_acl_open(struct hci_unit *unit, bdaddr_t *bdaddr)
73 struct hci_link *link;
74 struct hci_memo *memo;
75 hci_create_con_cp cp;
76 int err;
78 KKASSERT(unit != NULL);
79 KKASSERT(bdaddr != NULL);
81 link = hci_link_lookup_bdaddr(unit, bdaddr, HCI_LINK_ACL);
82 if (link == NULL) {
83 link = hci_link_alloc(unit);
84 if (link == NULL)
85 return NULL;
87 link->hl_type = HCI_LINK_ACL;
88 bdaddr_copy(&link->hl_bdaddr, bdaddr);
91 switch(link->hl_state) {
92 case HCI_LINK_CLOSED:
94 * open connection to remote device
96 memset(&cp, 0, sizeof(cp));
97 bdaddr_copy(&cp.bdaddr, bdaddr);
98 cp.pkt_type = htole16(unit->hci_packet_type);
100 memo = hci_memo_find(unit, bdaddr);
101 if (memo != NULL) {
102 cp.page_scan_rep_mode = memo->page_scan_rep_mode;
103 cp.page_scan_mode = memo->page_scan_mode;
104 cp.clock_offset = memo->clock_offset;
107 if (unit->hci_link_policy & HCI_LINK_POLICY_ENABLE_ROLE_SWITCH)
108 cp.accept_role_switch = 1;
110 err = hci_send_cmd(unit, HCI_CMD_CREATE_CON, &cp, sizeof(cp));
111 if (err) {
112 hci_link_free(link, err);
113 return NULL;
116 link->hl_state = HCI_LINK_WAIT_CONNECT;
117 break;
119 case HCI_LINK_WAIT_CONNECT:
120 case HCI_LINK_WAIT_AUTH:
121 case HCI_LINK_WAIT_ENCRYPT:
122 case HCI_LINK_WAIT_SECURE:
124 * somebody else already trying to connect, we just
125 * sit on the bench with them..
127 break;
129 case HCI_LINK_OPEN:
131 * If already open, halt any expiry callouts. We dont need
132 * to care about already invoking callouts since refcnt >0
133 * will keep the link alive.
135 callout_stop(&link->hl_expire);
136 break;
138 default:
139 UNKNOWN(link->hl_state);
140 return NULL;
143 /* open */
144 link->hl_refcnt++;
146 return link;
150 * Close ACL connection. When there are no more references to this link,
151 * we can either close it down or schedule a delayed closedown.
153 void
154 hci_acl_close(struct hci_link *link, int err)
156 KKASSERT(link != NULL);
158 if (--link->hl_refcnt == 0) {
159 if (link->hl_state == HCI_LINK_CLOSED)
160 hci_link_free(link, err);
161 else if (hci_acl_expiry > 0)
162 callout_reset(&link->hl_expire, hci_acl_expiry * hz,
163 hci_acl_timeout, link);
168 * Incoming ACL connection.
170 * For now, we accept all connections but it would be better to check
171 * the L2CAP listen list and only accept when there is a listener
172 * available.
174 * There should not be a link to the same bdaddr already, we check
175 * anyway though its left unhandled for now.
177 struct hci_link *
178 hci_acl_newconn(struct hci_unit *unit, bdaddr_t *bdaddr)
180 struct hci_link *link;
182 link = hci_link_lookup_bdaddr(unit, bdaddr, HCI_LINK_ACL);
183 if (link != NULL)
184 return NULL;
186 link = hci_link_alloc(unit);
187 if (link != NULL) {
188 link->hl_state = HCI_LINK_WAIT_CONNECT;
189 link->hl_type = HCI_LINK_ACL;
190 bdaddr_copy(&link->hl_bdaddr, bdaddr);
192 if (hci_acl_expiry > 0)
193 callout_reset(&link->hl_expire, hci_acl_expiry * hz,
194 hci_acl_timeout, link);
197 return link;
200 void
201 hci_acl_timeout(void *arg)
203 struct hci_link *link = arg;
204 hci_discon_cp cp;
205 int err;
207 crit_enter();
209 if (link->hl_refcnt > 0)
210 goto out;
212 DPRINTF("link #%d expired\n", link->hl_handle);
214 switch (link->hl_state) {
215 case HCI_LINK_CLOSED:
216 case HCI_LINK_WAIT_CONNECT:
217 hci_link_free(link, ECONNRESET);
218 break;
220 case HCI_LINK_WAIT_AUTH:
221 case HCI_LINK_WAIT_ENCRYPT:
222 case HCI_LINK_WAIT_SECURE:
223 case HCI_LINK_OPEN:
224 cp.con_handle = htole16(link->hl_handle);
225 cp.reason = 0x13; /* "Remote User Terminated Connection" */
227 err = hci_send_cmd(link->hl_unit, HCI_CMD_DISCONNECT,
228 &cp, sizeof(cp));
230 if (err) {
231 DPRINTF("error %d sending HCI_CMD_DISCONNECT\n",
232 err);
235 break;
237 default:
238 UNKNOWN(link->hl_state);
239 break;
242 out:
243 crit_exit();
247 * Initiate any Link Mode change requests.
250 hci_acl_setmode(struct hci_link *link)
252 int err;
254 KKASSERT(link != NULL);
255 KKASSERT(link->hl_unit != NULL);
257 if (link->hl_state != HCI_LINK_OPEN)
258 return EINPROGRESS;
260 if ((link->hl_flags & HCI_LINK_AUTH_REQ)
261 && !(link->hl_flags & HCI_LINK_AUTH)) {
262 hci_auth_req_cp cp;
264 DPRINTF("(%s) requesting auth for handle #%d\n",
265 device_get_nameunit(link->hl_unit->hci_dev),
266 link->hl_handle);
268 link->hl_state = HCI_LINK_WAIT_AUTH;
269 cp.con_handle = htole16(link->hl_handle);
270 err = hci_send_cmd(link->hl_unit, HCI_CMD_AUTH_REQ,
271 &cp, sizeof(cp));
273 return (err == 0 ? EINPROGRESS : err);
276 if ((link->hl_flags & HCI_LINK_ENCRYPT_REQ)
277 && !(link->hl_flags & HCI_LINK_ENCRYPT)) {
278 hci_set_con_encryption_cp cp;
280 /* XXX we should check features for encryption capability */
282 DPRINTF("(%s) requesting encryption for handle #%d\n",
283 device_get_nameunit(link->hl_unit->hci_dev),
284 link->hl_handle);
286 link->hl_state = HCI_LINK_WAIT_ENCRYPT;
287 cp.con_handle = htole16(link->hl_handle);
288 cp.encryption_enable = 0x01;
290 err = hci_send_cmd(link->hl_unit, HCI_CMD_SET_CON_ENCRYPTION,
291 &cp, sizeof(cp));
293 return (err == 0 ? EINPROGRESS : err);
296 if ((link->hl_flags & HCI_LINK_SECURE_REQ)) {
297 hci_change_con_link_key_cp cp;
299 /* always change link key for SECURE requests */
300 link->hl_flags &= ~HCI_LINK_SECURE;
302 DPRINTF("(%s) changing link key for handle #%d\n",
303 device_get_nameunit(link->hl_unit->hci_dev),
304 link->hl_handle);
306 link->hl_state = HCI_LINK_WAIT_SECURE;
307 cp.con_handle = htole16(link->hl_handle);
309 err = hci_send_cmd(link->hl_unit, HCI_CMD_CHANGE_CON_LINK_KEY,
310 &cp, sizeof(cp));
312 return (err == 0 ? EINPROGRESS : err);
315 return 0;
319 * Link Mode changed.
321 * This is called from event handlers when the mode change
322 * is complete. We notify upstream and restart the link.
324 void
325 hci_acl_linkmode(struct hci_link *link)
327 struct l2cap_channel *chan, *next;
328 int err, mode = 0;
330 DPRINTF("(%s) handle #%d, auth %s, encrypt %s, secure %s\n",
331 device_get_nameunit(link->hl_unit->hci_dev), link->hl_handle,
332 (link->hl_flags & HCI_LINK_AUTH ? "on" : "off"),
333 (link->hl_flags & HCI_LINK_ENCRYPT ? "on" : "off"),
334 (link->hl_flags & HCI_LINK_SECURE ? "on" : "off"));
336 if (link->hl_flags & HCI_LINK_AUTH)
337 mode |= L2CAP_LM_AUTH;
339 if (link->hl_flags & HCI_LINK_ENCRYPT)
340 mode |= L2CAP_LM_ENCRYPT;
342 if (link->hl_flags & HCI_LINK_SECURE)
343 mode |= L2CAP_LM_SECURE;
346 * The link state will only be OPEN here if the mode change
347 * was successful. So, we can proceed with L2CAP connections,
348 * or notify already establshed channels, to allow any that
349 * are dissatisfied to disconnect before we restart.
351 next = LIST_FIRST(&l2cap_active_list);
352 while ((chan = next) != NULL) {
353 next = LIST_NEXT(chan, lc_ncid);
355 if (chan->lc_link != link)
356 continue;
358 switch(chan->lc_state) {
359 case L2CAP_WAIT_SEND_CONNECT_REQ: /* we are connecting */
360 if ((mode & chan->lc_mode) != chan->lc_mode) {
361 l2cap_close(chan, ECONNABORTED);
362 break;
365 chan->lc_state = L2CAP_WAIT_RECV_CONNECT_RSP;
366 err = l2cap_send_connect_req(chan);
367 if (err) {
368 l2cap_close(chan, err);
369 break;
371 break;
373 case L2CAP_WAIT_SEND_CONNECT_RSP: /* they are connecting */
374 if ((mode & chan->lc_mode) != chan->lc_mode) {
375 l2cap_send_connect_rsp(link, chan->lc_ident,
376 0, chan->lc_rcid,
377 L2CAP_SECURITY_BLOCK);
379 l2cap_close(chan, ECONNABORTED);
380 break;
383 l2cap_send_connect_rsp(link, chan->lc_ident,
384 chan->lc_lcid, chan->lc_rcid,
385 L2CAP_SUCCESS);
387 chan->lc_state = L2CAP_WAIT_CONFIG;
388 chan->lc_flags |= (L2CAP_WAIT_CONFIG_RSP | L2CAP_WAIT_CONFIG_REQ);
389 err = l2cap_send_config_req(chan);
390 if (err) {
391 l2cap_close(chan, err);
392 break;
394 break;
396 case L2CAP_WAIT_RECV_CONNECT_RSP:
397 case L2CAP_WAIT_CONFIG:
398 case L2CAP_OPEN: /* already established */
399 (*chan->lc_proto->linkmode)(chan->lc_upper, mode);
400 break;
402 default:
403 break;
407 link->hl_state = HCI_LINK_OPEN;
408 hci_acl_start(link);
412 * Receive ACL Data
414 * we accumulate packet fragments on the hci_link structure
415 * until a full L2CAP frame is ready, then send it on.
417 void
418 hci_acl_recv(struct mbuf *m, struct hci_unit *unit)
420 struct hci_link *link;
421 hci_acldata_hdr_t hdr;
422 uint16_t handle, want;
423 int pb, got;
425 KKASSERT(m != NULL);
426 KKASSERT(unit != NULL);
428 KKASSERT(m->m_pkthdr.len >= sizeof(hdr));
429 m_copydata(m, 0, sizeof(hdr), (caddr_t)&hdr);
430 m_adj(m, sizeof(hdr));
432 #ifdef DIAGNOSTIC
433 if (hdr.type != HCI_ACL_DATA_PKT) {
434 kprintf("%s: bad ACL packet type\n",
435 device_get_nameunit(unit->hci_dev));
436 goto bad;
439 if (m->m_pkthdr.len != letoh16(hdr.length)) {
440 kprintf("%s: bad ACL packet length (%d != %d)\n",
441 device_get_nameunit(unit->hci_dev), m->m_pkthdr.len,
442 letoh16(hdr.length));
443 goto bad;
445 #endif
447 hdr.length = letoh16(hdr.length);
448 hdr.con_handle = letoh16(hdr.con_handle);
449 handle = HCI_CON_HANDLE(hdr.con_handle);
450 pb = HCI_PB_FLAG(hdr.con_handle);
452 link = hci_link_lookup_handle(unit, handle);
453 if (link == NULL) {
454 hci_discon_cp cp;
456 DPRINTF("%s: dumping packet for unknown handle #%d\n",
457 device_get_nameunit(unit->hci_dev), handle);
460 * There is no way to find out what this connection handle is
461 * for, just get rid of it. This may happen, if a USB dongle
462 * is plugged into a self powered hub and does not reset when
463 * the system is shut down.
465 cp.con_handle = htole16(handle);
466 cp.reason = 0x13; /* "Remote User Terminated Connection" */
467 hci_send_cmd(unit, HCI_CMD_DISCONNECT, &cp, sizeof(cp));
468 goto bad;
471 switch (pb) {
472 case HCI_PACKET_START:
473 if (link->hl_rxp != NULL)
474 kprintf("%s: dropped incomplete ACL packet\n",
475 device_get_nameunit(unit->hci_dev));
477 if (m->m_pkthdr.len < sizeof(l2cap_hdr_t)) {
478 kprintf("%s: short ACL packet\n",
479 device_get_nameunit(unit->hci_dev));
481 goto bad;
484 link->hl_rxp = m;
485 got = m->m_pkthdr.len;
486 break;
488 case HCI_PACKET_FRAGMENT:
489 if (link->hl_rxp == NULL) {
490 kprintf("%s: unexpected packet fragment\n",
491 device_get_nameunit(unit->hci_dev));
493 goto bad;
496 got = m->m_pkthdr.len + link->hl_rxp->m_pkthdr.len;
497 m_cat(link->hl_rxp, m);
498 m = link->hl_rxp;
499 m->m_pkthdr.len = got;
500 break;
502 default:
503 kprintf("%s: unknown packet type\n",
504 device_get_nameunit(unit->hci_dev));
506 goto bad;
509 m_copydata(m, 0, sizeof(want), (caddr_t)&want);
510 want = letoh16(want) + sizeof(l2cap_hdr_t) - got;
512 if (want > 0)
513 return;
515 link->hl_rxp = NULL;
517 if (want == 0) {
518 l2cap_recv_frame(m, link);
519 return;
522 bad:
523 m_freem(m);
527 * Send ACL data on link
529 * We must fragment packets into chunks of less than unit->hci_max_acl_size and
530 * prepend a relevant ACL header to each fragment. We keep a PDU structure
531 * attached to the link, so that completed fragments can be marked off and
532 * more data requested from above once the PDU is sent.
535 hci_acl_send(struct mbuf *m, struct hci_link *link,
536 struct l2cap_channel *chan)
538 struct l2cap_pdu *pdu;
539 struct mbuf *n = NULL;
540 int plen, mlen, num = 0;
542 KKASSERT(link != NULL);
543 KKASSERT(m != NULL);
544 KKASSERT(m->m_flags & M_PKTHDR);
545 KKASSERT(m->m_pkthdr.len > 0);
547 if (link->hl_state == HCI_LINK_CLOSED) {
548 m_freem(m);
549 return ENETDOWN;
552 pdu = zalloc(l2cap_pdu_pool);
553 if (pdu == NULL)
554 goto nomem;
556 bzero(pdu, sizeof *pdu);
557 pdu->lp_chan = chan;
558 pdu->lp_pending = 0;
560 plen = m->m_pkthdr.len;
561 mlen = link->hl_unit->hci_max_acl_size;
563 DPRINTFN(5, "%s: handle #%d, plen = %d, max = %d\n",
564 device_get_nameunit(link->hl_unit->hci_dev),
565 link->hl_handle, plen, mlen);
567 while (plen > 0) {
568 if (plen > mlen) {
569 n = m_split(m, mlen, MB_DONTWAIT);
570 if (n == NULL)
571 goto nomem;
572 } else {
573 mlen = plen;
576 if (num++ == 0)
577 m->m_flags |= M_PROTO1; /* tag first fragment */
579 DPRINTFN(10, "(%s) chunk of %d (plen = %d) bytes\n",
580 device_get_nameunit(link->hl_unit->hci_dev), mlen, plen);
581 IF_ENQUEUE(&pdu->lp_data, m);
582 m = n;
583 plen -= mlen;
586 TAILQ_INSERT_TAIL(&link->hl_txq, pdu, lp_next);
587 link->hl_txqlen += num;
589 hci_acl_start(link);
591 return 0;
593 nomem:
594 if (m) m_freem(m);
595 if (pdu) {
596 IF_DRAIN(&pdu->lp_data);
597 zfree(l2cap_pdu_pool, pdu);
600 return ENOMEM;
604 * Start sending ACL data on link.
606 * This is called when the queue may need restarting: as new data
607 * is queued, after link mode changes have completed, or when device
608 * buffers have cleared.
610 * We may use all the available packet slots. The reason that we add
611 * the ACL encapsulation here rather than in hci_acl_send() is that L2CAP
612 * signal packets may be queued before the handle is given to us..
614 void
615 hci_acl_start(struct hci_link *link)
617 struct hci_unit *unit;
618 hci_acldata_hdr_t *hdr;
619 struct l2cap_pdu *pdu;
620 struct mbuf *m;
621 uint16_t handle;
623 KKASSERT(link != NULL);
625 unit = link->hl_unit;
626 KKASSERT(unit != NULL);
628 /* this is mainly to block ourselves (below) */
629 if (link->hl_state != HCI_LINK_OPEN)
630 return;
632 if (link->hl_txqlen == 0 || unit->hci_num_acl_pkts == 0)
633 return;
635 /* find first PDU with data to send */
636 pdu = TAILQ_FIRST(&link->hl_txq);
637 for (;;) {
638 if (pdu == NULL)
639 return;
641 if (!IF_QEMPTY(&pdu->lp_data))
642 break;
644 pdu = TAILQ_NEXT(pdu, lp_next);
647 while (unit->hci_num_acl_pkts > 0) {
648 IF_DEQUEUE(&pdu->lp_data, m);
649 KKASSERT(m != NULL);
651 if (m->m_flags & M_PROTO1)
652 handle = HCI_MK_CON_HANDLE(link->hl_handle,
653 HCI_PACKET_START, 0);
654 else
655 handle = HCI_MK_CON_HANDLE(link->hl_handle,
656 HCI_PACKET_FRAGMENT, 0);
658 M_PREPEND(m, sizeof(*hdr), MB_DONTWAIT);
659 if (m == NULL)
660 break;
662 hdr = mtod(m, hci_acldata_hdr_t *);
663 hdr->type = HCI_ACL_DATA_PKT;
664 hdr->con_handle = htole16(handle);
665 hdr->length = htole16(m->m_pkthdr.len - sizeof(*hdr));
667 link->hl_txqlen--;
668 pdu->lp_pending++;
670 hci_output_acl(unit, m);
672 if (IF_QEMPTY(&pdu->lp_data)) {
673 if (pdu->lp_chan) {
675 * This should enable streaming of PDUs - when
676 * we have placed all the fragments on the acl
677 * output queue, we trigger the L2CAP layer to
678 * send us down one more. Use a false state so
679 * we dont run into ourselves coming back from
680 * the future..
682 link->hl_state = HCI_LINK_BLOCK;
683 l2cap_start(pdu->lp_chan);
684 link->hl_state = HCI_LINK_OPEN;
687 pdu = TAILQ_NEXT(pdu, lp_next);
688 if (pdu == NULL)
689 break;
694 * We had our turn now, move to the back of the queue to let
695 * other links have a go at the output buffers..
697 if (TAILQ_NEXT(link, hl_next)) {
698 TAILQ_REMOVE(&unit->hci_links, link, hl_next);
699 TAILQ_INSERT_TAIL(&unit->hci_links, link, hl_next);
704 * Confirm ACL packets cleared from Controller buffers. We scan our PDU
705 * list to clear pending fragments and signal upstream for more data
706 * when a PDU is complete.
708 void
709 hci_acl_complete(struct hci_link *link, int num)
711 struct l2cap_pdu *pdu;
712 struct l2cap_channel *chan;
714 DPRINTFN(5, "(%s) handle #%d (%d)\n",
715 device_get_nameunit(link->hl_unit->hci_dev), link->hl_handle, num);
717 while (num > 0) {
718 pdu = TAILQ_FIRST(&link->hl_txq);
719 if (pdu == NULL) {
720 kprintf("%s: %d packets completed on handle #%x "
721 "but none pending!\n",
722 device_get_nameunit(link->hl_unit->hci_dev),
723 num, link->hl_handle);
724 return;
727 if (num >= pdu->lp_pending) {
728 num -= pdu->lp_pending;
729 pdu->lp_pending = 0;
731 if (IF_QEMPTY(&pdu->lp_data)) {
732 TAILQ_REMOVE(&link->hl_txq, pdu, lp_next);
733 chan = pdu->lp_chan;
734 if (chan != NULL) {
735 chan->lc_pending--;
736 (*chan->lc_proto->complete)
737 (chan->lc_upper, 1);
739 if (chan->lc_pending == 0)
740 l2cap_start(chan);
743 zfree(l2cap_pdu_pool, pdu);
745 } else {
746 pdu->lp_pending -= num;
747 num = 0;
752 /*******************************************************************************
754 * HCI SCO Connections
758 * Incoming SCO Connection. We check the list for anybody willing
759 * to take it.
761 struct hci_link *
762 hci_sco_newconn(struct hci_unit *unit, bdaddr_t *bdaddr)
764 struct sockaddr_bt laddr, raddr;
765 struct sco_pcb *pcb, *new;
766 struct hci_link *sco, *acl;
768 memset(&laddr, 0, sizeof(laddr));
769 laddr.bt_len = sizeof(laddr);
770 laddr.bt_family = AF_BLUETOOTH;
771 bdaddr_copy(&laddr.bt_bdaddr, &unit->hci_bdaddr);
773 memset(&raddr, 0, sizeof(raddr));
774 raddr.bt_len = sizeof(raddr);
775 raddr.bt_family = AF_BLUETOOTH;
776 bdaddr_copy(&raddr.bt_bdaddr, bdaddr);
779 * There should already be an ACL link up and running before
780 * the controller sends us SCO connection requests, but you
781 * never know..
783 acl = hci_link_lookup_bdaddr(unit, bdaddr, HCI_LINK_ACL);
784 if (acl == NULL || acl->hl_state != HCI_LINK_OPEN)
785 return NULL;
787 LIST_FOREACH(pcb, &sco_pcb, sp_next) {
788 if ((pcb->sp_flags & SP_LISTENING) == 0)
789 continue;
791 new = (*pcb->sp_proto->newconn)(pcb->sp_upper, &laddr, &raddr);
792 if (new == NULL)
793 continue;
796 * Ok, got new pcb so we can start a new link and fill
797 * in all the details.
799 bdaddr_copy(&new->sp_laddr, &unit->hci_bdaddr);
800 bdaddr_copy(&new->sp_raddr, bdaddr);
802 sco = hci_link_alloc(unit);
803 if (sco == NULL) {
804 sco_detach(&new);
805 return NULL;
808 sco->hl_type = HCI_LINK_SCO;
809 bdaddr_copy(&sco->hl_bdaddr, bdaddr);
811 sco->hl_link = hci_acl_open(unit, bdaddr);
812 KKASSERT(sco->hl_link == acl);
814 sco->hl_sco = new;
815 new->sp_link = sco;
817 new->sp_mtu = unit->hci_max_sco_size;
818 return sco;
821 return NULL;
825 * receive SCO packet, we only need to strip the header and send
826 * it to the right handler
828 void
829 hci_sco_recv(struct mbuf *m, struct hci_unit *unit)
831 struct hci_link *link;
832 hci_scodata_hdr_t hdr;
833 uint16_t handle;
835 KKASSERT(m != NULL);
836 KKASSERT(unit != NULL);
838 KKASSERT(m->m_pkthdr.len >= sizeof(hdr));
839 m_copydata(m, 0, sizeof(hdr), (caddr_t)&hdr);
840 m_adj(m, sizeof(hdr));
842 #ifdef DIAGNOSTIC
843 if (hdr.type != HCI_SCO_DATA_PKT) {
844 kprintf("%s: bad SCO packet type\n",
845 device_get_nameunit(unit->hci_dev));
846 goto bad;
849 if (m->m_pkthdr.len != hdr.length) {
850 kprintf("%s: bad SCO packet length (%d != %d)\n",
851 device_get_nameunit(unit->hci_dev), m->m_pkthdr.len,
852 hdr.length);
853 goto bad;
855 #endif
857 hdr.con_handle = letoh16(hdr.con_handle);
858 handle = HCI_CON_HANDLE(hdr.con_handle);
860 link = hci_link_lookup_handle(unit, handle);
861 if (link == NULL || link->hl_type == HCI_LINK_ACL) {
862 DPRINTF("%s: dumping packet for unknown handle #%d\n",
863 device_get_nameunit(unit->hci_dev), handle);
865 goto bad;
868 (*link->hl_sco->sp_proto->input)(link->hl_sco->sp_upper, m);
869 return;
871 bad:
872 m_freem(m);
875 void
876 hci_sco_start(struct hci_link *link)
881 * SCO packets have completed at the controller, so we can
882 * signal up to free the buffer space.
884 void
885 hci_sco_complete(struct hci_link *link, int num)
888 DPRINTFN(5, "handle #%d (num=%d)\n", link->hl_handle, num);
889 link->hl_sco->sp_pending--;
890 (*link->hl_sco->sp_proto->complete)(link->hl_sco->sp_upper, num);
893 /*******************************************************************************
895 * Generic HCI Connection alloc/free/lookup etc
898 struct hci_link *
899 hci_link_alloc(struct hci_unit *unit)
901 struct hci_link *link;
903 KKASSERT(unit != NULL);
905 link = kmalloc(sizeof *link, M_BLUETOOTH, M_NOWAIT | M_ZERO);
906 if (link == NULL)
907 return NULL;
909 link->hl_unit = unit;
910 link->hl_state = HCI_LINK_CLOSED;
912 /* init ACL portion */
913 callout_init(&link->hl_expire);
915 crit_enter();
916 TAILQ_INIT(&link->hl_txq); /* outgoing packets */
917 TAILQ_INIT(&link->hl_reqs); /* request queue */
919 link->hl_mtu = L2CAP_MTU_DEFAULT; /* L2CAP signal mtu */
920 link->hl_flush = L2CAP_FLUSH_TIMO_DEFAULT; /* flush timeout */
922 /* init SCO portion */
923 /* &link->hl_data is already zero-initialized. */
925 /* attach to unit */
926 TAILQ_INSERT_HEAD(&unit->hci_links, link, hl_next);
927 crit_exit();
928 return link;
931 void
932 hci_link_free(struct hci_link *link, int err)
934 struct l2cap_req *req;
935 struct l2cap_pdu *pdu;
936 struct l2cap_channel *chan, *next;
938 KKASSERT(link != NULL);
940 DPRINTF("(%s) #%d, type = %d, state = %d, refcnt = %d\n",
941 device_get_nameunit(link->hl_unit->hci_dev), link->hl_handle,
942 link->hl_type, link->hl_state, link->hl_refcnt);
944 /* ACL reference count */
945 if (link->hl_refcnt > 0) {
946 next = LIST_FIRST(&l2cap_active_list);
947 while ((chan = next) != NULL) {
948 next = LIST_NEXT(chan, lc_ncid);
949 if (chan->lc_link == link)
950 l2cap_close(chan, err);
953 KKASSERT(link->hl_refcnt == 0);
955 /* ACL L2CAP requests.. */
956 while ((req = TAILQ_FIRST(&link->hl_reqs)) != NULL)
957 l2cap_request_free(req);
959 KKASSERT(TAILQ_EMPTY(&link->hl_reqs));
961 /* ACL outgoing data queue */
962 while ((pdu = TAILQ_FIRST(&link->hl_txq)) != NULL) {
963 TAILQ_REMOVE(&link->hl_txq, pdu, lp_next);
964 IF_DRAIN(&pdu->lp_data);
965 if (pdu->lp_pending)
966 link->hl_unit->hci_num_acl_pkts += pdu->lp_pending;
968 zfree(l2cap_pdu_pool, pdu);
971 KKASSERT(TAILQ_EMPTY(&link->hl_txq));
973 /* ACL incoming data packet */
974 if (link->hl_rxp != NULL) {
975 m_freem(link->hl_rxp);
976 link->hl_rxp = NULL;
979 /* SCO master ACL link */
980 if (link->hl_link != NULL) {
981 hci_acl_close(link->hl_link, err);
982 link->hl_link = NULL;
985 /* SCO pcb */
986 if (link->hl_sco != NULL) {
987 struct sco_pcb *pcb;
989 pcb = link->hl_sco;
990 pcb->sp_link = NULL;
991 link->hl_sco = NULL;
992 (*pcb->sp_proto->disconnected)(pcb->sp_upper, err);
995 /* flush any SCO data */
996 crit_enter();
997 IF_DRAIN(&link->hl_data);
998 crit_exit();
1001 * Halt the timeout - if its already running we cannot free the
1002 * link structure but the timeout function will call us back in
1003 * any case.
1005 link->hl_state = HCI_LINK_CLOSED;
1006 callout_stop(&link->hl_expire);
1007 if (callout_active(&link->hl_expire))
1008 return;
1011 * If we made a note of clock offset, keep it in a memo
1012 * to facilitate reconnections to this device
1014 if (link->hl_clock != 0) {
1015 struct hci_memo *memo;
1017 memo = hci_memo_new(link->hl_unit, &link->hl_bdaddr);
1018 if (memo != NULL)
1019 memo->clock_offset = link->hl_clock;
1022 crit_enter();
1023 TAILQ_REMOVE(&link->hl_unit->hci_links, link, hl_next);
1024 crit_exit();
1025 kfree(link, M_BLUETOOTH);
1029 * Lookup HCI link by type and state.
1031 struct hci_link *
1032 hci_link_lookup_state(struct hci_unit *unit, uint16_t type, uint16_t state)
1034 struct hci_link *link;
1036 TAILQ_FOREACH(link, &unit->hci_links, hl_next) {
1037 if (link->hl_type == type && link->hl_state == state)
1038 break;
1041 return link;
1045 * Lookup HCI link by address and type. Note that for SCO links there may
1046 * be more than one link per address, so we only return links with no
1047 * handle (ie new links)
1049 struct hci_link *
1050 hci_link_lookup_bdaddr(struct hci_unit *unit, bdaddr_t *bdaddr, uint16_t type)
1052 struct hci_link *link;
1054 KKASSERT(unit != NULL);
1055 KKASSERT(bdaddr != NULL);
1057 TAILQ_FOREACH(link, &unit->hci_links, hl_next) {
1058 if (link->hl_type != type)
1059 continue;
1061 if (type == HCI_LINK_SCO && link->hl_handle != 0)
1062 continue;
1064 if (bdaddr_same(&link->hl_bdaddr, bdaddr))
1065 break;
1068 return link;
1071 struct hci_link *
1072 hci_link_lookup_handle(struct hci_unit *unit, uint16_t handle)
1074 struct hci_link *link;
1076 KKASSERT(unit != NULL);
1078 TAILQ_FOREACH(link, &unit->hci_links, hl_next) {
1079 if (handle == link->hl_handle)
1080 break;
1083 return link;