hci_link.c revision 1.17 1 /* $NetBSD: hci_link.c,v 1.17 2008/03/06 20:56:26 plunky Exp $ */
2
3 /*-
4 * Copyright (c) 2005 Iain Hibbert.
5 * Copyright (c) 2006 Itronix Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of Itronix Inc. may not be used to endorse
17 * or promote products derived from this software without specific
18 * prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: hci_link.c,v 1.17 2008/03/06 20:56:26 plunky Exp $");
35
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/proc.h>
41 #include <sys/queue.h>
42 #include <sys/systm.h>
43
44 #include <netbt/bluetooth.h>
45 #include <netbt/hci.h>
46 #include <netbt/l2cap.h>
47 #include <netbt/sco.h>
48
49 /*******************************************************************************
50 *
51 * HCI ACL Connections
52 */
53
54 /*
55 * Automatically expire unused ACL connections after this number of
56 * seconds (if zero, do not expire unused connections) [sysctl]
57 */
58 int hci_acl_expiry = 10; /* seconds */
59
60 /*
61 * hci_acl_open(unit, bdaddr)
62 *
63 * open ACL connection to remote bdaddr. Only one ACL connection is permitted
64 * between any two Bluetooth devices, so we look for an existing one before
65 * trying to start a new one.
66 */
67 struct hci_link *
68 hci_acl_open(struct hci_unit *unit, bdaddr_t *bdaddr)
69 {
70 struct hci_link *link;
71 struct hci_memo *memo;
72 hci_create_con_cp cp;
73 int err;
74
75 KASSERT(unit != NULL);
76 KASSERT(bdaddr != NULL);
77
78 link = hci_link_lookup_bdaddr(unit, bdaddr, HCI_LINK_ACL);
79 if (link == NULL) {
80 link = hci_link_alloc(unit);
81 if (link == NULL)
82 return NULL;
83
84 link->hl_type = HCI_LINK_ACL;
85 bdaddr_copy(&link->hl_bdaddr, bdaddr);
86 }
87
88 switch(link->hl_state) {
89 case HCI_LINK_CLOSED:
90 /*
91 * open connection to remote device
92 */
93 memset(&cp, 0, sizeof(cp));
94 bdaddr_copy(&cp.bdaddr, bdaddr);
95 cp.pkt_type = htole16(unit->hci_packet_type);
96
97 memo = hci_memo_find(unit, bdaddr);
98 if (memo != NULL) {
99 cp.page_scan_rep_mode = memo->page_scan_rep_mode;
100 cp.page_scan_mode = memo->page_scan_mode;
101 cp.clock_offset = memo->clock_offset;
102 }
103
104 if (unit->hci_link_policy & HCI_LINK_POLICY_ENABLE_ROLE_SWITCH)
105 cp.accept_role_switch = 1;
106
107 err = hci_send_cmd(unit, HCI_CMD_CREATE_CON, &cp, sizeof(cp));
108 if (err) {
109 hci_link_free(link, err);
110 return NULL;
111 }
112
113 link->hl_flags |= HCI_LINK_CREATE_CON;
114 link->hl_state = HCI_LINK_WAIT_CONNECT;
115 break;
116
117 case HCI_LINK_WAIT_CONNECT:
118 case HCI_LINK_WAIT_AUTH:
119 case HCI_LINK_WAIT_ENCRYPT:
120 case HCI_LINK_WAIT_SECURE:
121 /*
122 * somebody else already trying to connect, we just
123 * sit on the bench with them..
124 */
125 break;
126
127 case HCI_LINK_OPEN:
128 /*
129 * If already open, halt any expiry timeouts. We dont need
130 * to care about already invoking timeouts since refcnt >0
131 * will keep the link alive.
132 */
133 callout_stop(&link->hl_expire);
134 break;
135
136 default:
137 UNKNOWN(link->hl_state);
138 return NULL;
139 }
140
141 /* open */
142 link->hl_refcnt++;
143
144 return link;
145 }
146
147 /*
148 * Close ACL connection. When there are no more references to this link,
149 * we can either close it down or schedule a delayed closedown.
150 */
151 void
152 hci_acl_close(struct hci_link *link, int err)
153 {
154
155 KASSERT(link != NULL);
156
157 if (--link->hl_refcnt == 0) {
158 if (link->hl_state == HCI_LINK_CLOSED)
159 hci_link_free(link, err);
160 else if (hci_acl_expiry > 0)
161 callout_schedule(&link->hl_expire, hci_acl_expiry * hz);
162 }
163 }
164
165 /*
166 * Incoming ACL connection.
167 *
168 * For now, we accept all connections but it would be better to check
169 * the L2CAP listen list and only accept when there is a listener
170 * available.
171 *
172 * There should not be a link to the same bdaddr already, we check
173 * anyway though its left unhandled for now.
174 */
175 struct hci_link *
176 hci_acl_newconn(struct hci_unit *unit, bdaddr_t *bdaddr)
177 {
178 struct hci_link *link;
179
180 link = hci_link_lookup_bdaddr(unit, bdaddr, HCI_LINK_ACL);
181 if (link != NULL)
182 return NULL;
183
184 link = hci_link_alloc(unit);
185 if (link != NULL) {
186 link->hl_state = HCI_LINK_WAIT_CONNECT;
187 link->hl_type = HCI_LINK_ACL;
188 bdaddr_copy(&link->hl_bdaddr, bdaddr);
189
190 if (hci_acl_expiry > 0)
191 callout_schedule(&link->hl_expire, hci_acl_expiry * hz);
192 }
193
194 return link;
195 }
196
197 void
198 hci_acl_timeout(void *arg)
199 {
200 struct hci_link *link = arg;
201 hci_discon_cp cp;
202 int s, err;
203
204 s = splsoftnet();
205 callout_ack(&link->hl_expire);
206
207 if (link->hl_refcnt > 0)
208 goto out;
209
210 DPRINTF("link #%d expired\n", link->hl_handle);
211
212 switch (link->hl_state) {
213 case HCI_LINK_CLOSED:
214 case HCI_LINK_WAIT_CONNECT:
215 hci_link_free(link, ECONNRESET);
216 break;
217
218 case HCI_LINK_WAIT_AUTH:
219 case HCI_LINK_WAIT_ENCRYPT:
220 case HCI_LINK_WAIT_SECURE:
221 case HCI_LINK_OPEN:
222 cp.con_handle = htole16(link->hl_handle);
223 cp.reason = 0x13; /* "Remote User Terminated Connection" */
224
225 err = hci_send_cmd(link->hl_unit, HCI_CMD_DISCONNECT,
226 &cp, sizeof(cp));
227
228 if (err) {
229 DPRINTF("error %d sending HCI_CMD_DISCONNECT\n",
230 err);
231 }
232
233 break;
234
235 default:
236 UNKNOWN(link->hl_state);
237 break;
238 }
239
240 out:
241 splx(s);
242 }
243
244 /*
245 * Initiate any Link Mode change requests.
246 */
247 int
248 hci_acl_setmode(struct hci_link *link)
249 {
250 int err;
251
252 KASSERT(link != NULL);
253 KASSERT(link->hl_unit != NULL);
254
255 if (link->hl_state != HCI_LINK_OPEN)
256 return EINPROGRESS;
257
258 if ((link->hl_flags & HCI_LINK_AUTH_REQ)
259 && !(link->hl_flags & HCI_LINK_AUTH)) {
260 hci_auth_req_cp cp;
261
262 DPRINTF("requesting auth for handle #%d\n",
263 link->hl_handle);
264
265 link->hl_state = HCI_LINK_WAIT_AUTH;
266 cp.con_handle = htole16(link->hl_handle);
267 err = hci_send_cmd(link->hl_unit, HCI_CMD_AUTH_REQ,
268 &cp, sizeof(cp));
269
270 return (err == 0 ? EINPROGRESS : err);
271 }
272
273 if ((link->hl_flags & HCI_LINK_ENCRYPT_REQ)
274 && !(link->hl_flags & HCI_LINK_ENCRYPT)) {
275 hci_set_con_encryption_cp cp;
276
277 /* XXX we should check features for encryption capability */
278
279 DPRINTF("requesting encryption for handle #%d\n",
280 link->hl_handle);
281
282 link->hl_state = HCI_LINK_WAIT_ENCRYPT;
283 cp.con_handle = htole16(link->hl_handle);
284 cp.encryption_enable = 0x01;
285
286 err = hci_send_cmd(link->hl_unit, HCI_CMD_SET_CON_ENCRYPTION,
287 &cp, sizeof(cp));
288
289 return (err == 0 ? EINPROGRESS : err);
290 }
291
292 if ((link->hl_flags & HCI_LINK_SECURE_REQ)) {
293 hci_change_con_link_key_cp cp;
294
295 /* always change link key for SECURE requests */
296 link->hl_flags &= ~HCI_LINK_SECURE;
297
298 DPRINTF("changing link key for handle #%d\n",
299 link->hl_handle);
300
301 link->hl_state = HCI_LINK_WAIT_SECURE;
302 cp.con_handle = htole16(link->hl_handle);
303
304 err = hci_send_cmd(link->hl_unit, HCI_CMD_CHANGE_CON_LINK_KEY,
305 &cp, sizeof(cp));
306
307 return (err == 0 ? EINPROGRESS : err);
308 }
309
310 return 0;
311 }
312
313 /*
314 * Link Mode changed.
315 *
316 * This is called from event handlers when the mode change
317 * is complete. We notify upstream and restart the link.
318 */
319 void
320 hci_acl_linkmode(struct hci_link *link)
321 {
322 struct l2cap_channel *chan, *next;
323 int err, mode = 0;
324
325 DPRINTF("handle #%d, auth %s, encrypt %s, secure %s\n",
326 link->hl_handle,
327 (link->hl_flags & HCI_LINK_AUTH ? "on" : "off"),
328 (link->hl_flags & HCI_LINK_ENCRYPT ? "on" : "off"),
329 (link->hl_flags & HCI_LINK_SECURE ? "on" : "off"));
330
331 if (link->hl_flags & HCI_LINK_AUTH)
332 mode |= L2CAP_LM_AUTH;
333
334 if (link->hl_flags & HCI_LINK_ENCRYPT)
335 mode |= L2CAP_LM_ENCRYPT;
336
337 if (link->hl_flags & HCI_LINK_SECURE)
338 mode |= L2CAP_LM_SECURE;
339
340 /*
341 * The link state will only be OPEN here if the mode change
342 * was successful. So, we can proceed with L2CAP connections,
343 * or notify already establshed channels, to allow any that
344 * are dissatisfied to disconnect before we restart.
345 */
346 next = LIST_FIRST(&l2cap_active_list);
347 while ((chan = next) != NULL) {
348 next = LIST_NEXT(chan, lc_ncid);
349
350 if (chan->lc_link != link)
351 continue;
352
353 switch(chan->lc_state) {
354 case L2CAP_WAIT_SEND_CONNECT_REQ: /* we are connecting */
355 if ((mode & chan->lc_mode) != chan->lc_mode) {
356 l2cap_close(chan, ECONNABORTED);
357 break;
358 }
359
360 chan->lc_state = L2CAP_WAIT_RECV_CONNECT_RSP;
361 err = l2cap_send_connect_req(chan);
362 if (err) {
363 l2cap_close(chan, err);
364 break;
365 }
366 break;
367
368 case L2CAP_WAIT_SEND_CONNECT_RSP: /* they are connecting */
369 if ((mode & chan->lc_mode) != chan->lc_mode) {
370 l2cap_send_connect_rsp(link, chan->lc_ident,
371 0, chan->lc_rcid,
372 L2CAP_SECURITY_BLOCK);
373
374 l2cap_close(chan, ECONNABORTED);
375 break;
376 }
377
378 l2cap_send_connect_rsp(link, chan->lc_ident,
379 chan->lc_lcid, chan->lc_rcid,
380 L2CAP_SUCCESS);
381
382 chan->lc_state = L2CAP_WAIT_CONFIG;
383 chan->lc_flags |= (L2CAP_WAIT_CONFIG_RSP | L2CAP_WAIT_CONFIG_REQ);
384 err = l2cap_send_config_req(chan);
385 if (err) {
386 l2cap_close(chan, err);
387 break;
388 }
389 break;
390
391 case L2CAP_WAIT_RECV_CONNECT_RSP:
392 case L2CAP_WAIT_CONFIG:
393 case L2CAP_OPEN: /* already established */
394 (*chan->lc_proto->linkmode)(chan->lc_upper, mode);
395 break;
396
397 default:
398 break;
399 }
400 }
401
402 link->hl_state = HCI_LINK_OPEN;
403 hci_acl_start(link);
404 }
405
406 /*
407 * Receive ACL Data
408 *
409 * we accumulate packet fragments on the hci_link structure
410 * until a full L2CAP frame is ready, then send it on.
411 */
412 void
413 hci_acl_recv(struct mbuf *m, struct hci_unit *unit)
414 {
415 struct hci_link *link;
416 hci_acldata_hdr_t hdr;
417 uint16_t handle, want;
418 int pb, got;
419
420 KASSERT(m != NULL);
421 KASSERT(unit != NULL);
422
423 KASSERT(m->m_pkthdr.len >= sizeof(hdr));
424 m_copydata(m, 0, sizeof(hdr), &hdr);
425 m_adj(m, sizeof(hdr));
426
427 #ifdef DIAGNOSTIC
428 if (hdr.type != HCI_ACL_DATA_PKT) {
429 aprint_error_dev(unit->hci_dev, "bad ACL packet type\n");
430 goto bad;
431 }
432
433 if (m->m_pkthdr.len != le16toh(hdr.length)) {
434 aprint_error_dev(unit->hci_dev,
435 "bad ACL packet length (%d != %d)\n",
436 m->m_pkthdr.len, le16toh(hdr.length));
437 goto bad;
438 }
439 #endif
440
441 hdr.length = le16toh(hdr.length);
442 hdr.con_handle = le16toh(hdr.con_handle);
443 handle = HCI_CON_HANDLE(hdr.con_handle);
444 pb = HCI_PB_FLAG(hdr.con_handle);
445
446 link = hci_link_lookup_handle(unit, handle);
447 if (link == NULL) {
448 hci_discon_cp cp;
449
450 DPRINTF("%s: dumping packet for unknown handle #%d\n",
451 device_xname(unit->hci_dev), handle);
452
453 /*
454 * There is no way to find out what this connection handle is
455 * for, just get rid of it. This may happen, if a USB dongle
456 * is plugged into a self powered hub and does not reset when
457 * the system is shut down.
458 */
459 cp.con_handle = htole16(handle);
460 cp.reason = 0x13; /* "Remote User Terminated Connection" */
461 hci_send_cmd(unit, HCI_CMD_DISCONNECT, &cp, sizeof(cp));
462 goto bad;
463 }
464
465 switch (pb) {
466 case HCI_PACKET_START:
467 if (link->hl_rxp != NULL)
468 aprint_error_dev(unit->hci_dev,
469 "dropped incomplete ACL packet\n");
470
471 if (m->m_pkthdr.len < sizeof(l2cap_hdr_t)) {
472 aprint_error_dev(unit->hci_dev, "short ACL packet\n");
473 goto bad;
474 }
475
476 link->hl_rxp = m;
477 got = m->m_pkthdr.len;
478 break;
479
480 case HCI_PACKET_FRAGMENT:
481 if (link->hl_rxp == NULL) {
482 aprint_error_dev(unit->hci_dev,
483 "unexpected packet fragment\n");
484
485 goto bad;
486 }
487
488 got = m->m_pkthdr.len + link->hl_rxp->m_pkthdr.len;
489 m_cat(link->hl_rxp, m);
490 m = link->hl_rxp;
491 m->m_pkthdr.len = got;
492 break;
493
494 default:
495 aprint_error_dev(unit->hci_dev, "unknown packet type\n");
496 goto bad;
497 }
498
499 m_copydata(m, 0, sizeof(want), &want);
500 want = le16toh(want) + sizeof(l2cap_hdr_t) - got;
501
502 if (want > 0)
503 return;
504
505 link->hl_rxp = NULL;
506
507 if (want == 0) {
508 l2cap_recv_frame(m, link);
509 return;
510 }
511
512 bad:
513 m_freem(m);
514 }
515
516 /*
517 * Send ACL data on link
518 *
519 * We must fragment packets into chunks of less than unit->hci_max_acl_size and
520 * prepend a relevant ACL header to each fragment. We keep a PDU structure
521 * attached to the link, so that completed fragments can be marked off and
522 * more data requested from above once the PDU is sent.
523 */
524 int
525 hci_acl_send(struct mbuf *m, struct hci_link *link,
526 struct l2cap_channel *chan)
527 {
528 struct l2cap_pdu *pdu;
529 struct mbuf *n = NULL;
530 int plen, mlen, num = 0;
531
532 KASSERT(link != NULL);
533 KASSERT(m != NULL);
534 KASSERT(m->m_flags & M_PKTHDR);
535 KASSERT(m->m_pkthdr.len > 0);
536
537 if (link->hl_state == HCI_LINK_CLOSED) {
538 m_freem(m);
539 return ENETDOWN;
540 }
541
542 pdu = pool_get(&l2cap_pdu_pool, PR_NOWAIT);
543 if (pdu == NULL)
544 goto nomem;
545
546 pdu->lp_chan = chan;
547 pdu->lp_pending = 0;
548 MBUFQ_INIT(&pdu->lp_data);
549
550 plen = m->m_pkthdr.len;
551 mlen = link->hl_unit->hci_max_acl_size;
552
553 DPRINTFN(5, "%s: handle #%d, plen = %d, max = %d\n",
554 device_xname(link->hl_unit->hci_dev), link->hl_handle, plen, mlen);
555
556 while (plen > 0) {
557 if (plen > mlen) {
558 n = m_split(m, mlen, M_DONTWAIT);
559 if (n == NULL)
560 goto nomem;
561 } else {
562 mlen = plen;
563 }
564
565 if (num++ == 0)
566 m->m_flags |= M_PROTO1; /* tag first fragment */
567
568 DPRINTFN(10, "chunk of %d (plen = %d) bytes\n", mlen, plen);
569 MBUFQ_ENQUEUE(&pdu->lp_data, m);
570 m = n;
571 plen -= mlen;
572 }
573
574 TAILQ_INSERT_TAIL(&link->hl_txq, pdu, lp_next);
575 link->hl_txqlen += num;
576
577 hci_acl_start(link);
578
579 return 0;
580
581 nomem:
582 if (m) m_freem(m);
583 if (pdu) {
584 MBUFQ_DRAIN(&pdu->lp_data);
585 pool_put(&l2cap_pdu_pool, pdu);
586 }
587
588 return ENOMEM;
589 }
590
591 /*
592 * Start sending ACL data on link.
593 *
594 * This is called when the queue may need restarting: as new data
595 * is queued, after link mode changes have completed, or when device
596 * buffers have cleared.
597 *
598 * We may use all the available packet slots. The reason that we add
599 * the ACL encapsulation here rather than in hci_acl_send() is that L2CAP
600 * signal packets may be queued before the handle is given to us..
601 */
602 void
603 hci_acl_start(struct hci_link *link)
604 {
605 struct hci_unit *unit;
606 hci_acldata_hdr_t *hdr;
607 struct l2cap_pdu *pdu;
608 struct mbuf *m;
609 uint16_t handle;
610
611 KASSERT(link != NULL);
612
613 unit = link->hl_unit;
614 KASSERT(unit != NULL);
615
616 /* this is mainly to block ourselves (below) */
617 if (link->hl_state != HCI_LINK_OPEN)
618 return;
619
620 if (link->hl_txqlen == 0 || unit->hci_num_acl_pkts == 0)
621 return;
622
623 /* find first PDU with data to send */
624 pdu = TAILQ_FIRST(&link->hl_txq);
625 for (;;) {
626 if (pdu == NULL)
627 return;
628
629 if (MBUFQ_FIRST(&pdu->lp_data) != NULL)
630 break;
631
632 pdu = TAILQ_NEXT(pdu, lp_next);
633 }
634
635 while (unit->hci_num_acl_pkts > 0) {
636 MBUFQ_DEQUEUE(&pdu->lp_data, m);
637 KASSERT(m != NULL);
638
639 if (m->m_flags & M_PROTO1)
640 handle = HCI_MK_CON_HANDLE(link->hl_handle,
641 HCI_PACKET_START, 0);
642 else
643 handle = HCI_MK_CON_HANDLE(link->hl_handle,
644 HCI_PACKET_FRAGMENT, 0);
645
646 M_PREPEND(m, sizeof(*hdr), M_DONTWAIT);
647 if (m == NULL)
648 break;
649
650 hdr = mtod(m, hci_acldata_hdr_t *);
651 hdr->type = HCI_ACL_DATA_PKT;
652 hdr->con_handle = htole16(handle);
653 hdr->length = htole16(m->m_pkthdr.len - sizeof(*hdr));
654
655 link->hl_txqlen--;
656 pdu->lp_pending++;
657
658 hci_output_acl(unit, m);
659
660 if (MBUFQ_FIRST(&pdu->lp_data) == NULL) {
661 if (pdu->lp_chan) {
662 /*
663 * This should enable streaming of PDUs - when
664 * we have placed all the fragments on the acl
665 * output queue, we trigger the L2CAP layer to
666 * send us down one more. Use a false state so
667 * we dont run into ourselves coming back from
668 * the future..
669 */
670 link->hl_state = HCI_LINK_BLOCK;
671 l2cap_start(pdu->lp_chan);
672 link->hl_state = HCI_LINK_OPEN;
673 }
674
675 pdu = TAILQ_NEXT(pdu, lp_next);
676 if (pdu == NULL)
677 break;
678 }
679 }
680
681 /*
682 * We had our turn now, move to the back of the queue to let
683 * other links have a go at the output buffers..
684 */
685 if (TAILQ_NEXT(link, hl_next)) {
686 TAILQ_REMOVE(&unit->hci_links, link, hl_next);
687 TAILQ_INSERT_TAIL(&unit->hci_links, link, hl_next);
688 }
689 }
690
691 /*
692 * Confirm ACL packets cleared from Controller buffers. We scan our PDU
693 * list to clear pending fragments and signal upstream for more data
694 * when a PDU is complete.
695 */
696 void
697 hci_acl_complete(struct hci_link *link, int num)
698 {
699 struct l2cap_pdu *pdu;
700 struct l2cap_channel *chan;
701
702 DPRINTFN(5, "handle #%d (%d)\n", link->hl_handle, num);
703
704 while (num > 0) {
705 pdu = TAILQ_FIRST(&link->hl_txq);
706 if (pdu == NULL) {
707 aprint_error_dev(link->hl_unit->hci_dev,
708 "%d packets completed on handle #%x but none pending!\n",
709 num, link->hl_handle);
710
711 return;
712 }
713
714 if (num >= pdu->lp_pending) {
715 num -= pdu->lp_pending;
716 pdu->lp_pending = 0;
717
718 if (MBUFQ_FIRST(&pdu->lp_data) == NULL) {
719 TAILQ_REMOVE(&link->hl_txq, pdu, lp_next);
720 chan = pdu->lp_chan;
721 if (chan != NULL) {
722 chan->lc_pending--;
723 (*chan->lc_proto->complete)
724 (chan->lc_upper, 1);
725
726 if (chan->lc_pending == 0)
727 l2cap_start(chan);
728 }
729
730 pool_put(&l2cap_pdu_pool, pdu);
731 }
732 } else {
733 pdu->lp_pending -= num;
734 num = 0;
735 }
736 }
737 }
738
739 /*******************************************************************************
740 *
741 * HCI SCO Connections
742 */
743
744 /*
745 * Incoming SCO Connection. We check the list for anybody willing
746 * to take it.
747 */
748 struct hci_link *
749 hci_sco_newconn(struct hci_unit *unit, bdaddr_t *bdaddr)
750 {
751 struct sockaddr_bt laddr, raddr;
752 struct sco_pcb *pcb, *new;
753 struct hci_link *sco, *acl;
754
755 memset(&laddr, 0, sizeof(laddr));
756 laddr.bt_len = sizeof(laddr);
757 laddr.bt_family = AF_BLUETOOTH;
758 bdaddr_copy(&laddr.bt_bdaddr, &unit->hci_bdaddr);
759
760 memset(&raddr, 0, sizeof(raddr));
761 raddr.bt_len = sizeof(raddr);
762 raddr.bt_family = AF_BLUETOOTH;
763 bdaddr_copy(&raddr.bt_bdaddr, bdaddr);
764
765 /*
766 * There should already be an ACL link up and running before
767 * the controller sends us SCO connection requests, but you
768 * never know..
769 */
770 acl = hci_link_lookup_bdaddr(unit, bdaddr, HCI_LINK_ACL);
771 if (acl == NULL || acl->hl_state != HCI_LINK_OPEN)
772 return NULL;
773
774 LIST_FOREACH(pcb, &sco_pcb, sp_next) {
775 if ((pcb->sp_flags & SP_LISTENING) == 0)
776 continue;
777
778 new = (*pcb->sp_proto->newconn)(pcb->sp_upper, &laddr, &raddr);
779 if (new == NULL)
780 continue;
781
782 /*
783 * Ok, got new pcb so we can start a new link and fill
784 * in all the details.
785 */
786 bdaddr_copy(&new->sp_laddr, &unit->hci_bdaddr);
787 bdaddr_copy(&new->sp_raddr, bdaddr);
788
789 sco = hci_link_alloc(unit);
790 if (sco == NULL) {
791 sco_detach(&new);
792 return NULL;
793 }
794
795 sco->hl_type = HCI_LINK_SCO;
796 bdaddr_copy(&sco->hl_bdaddr, bdaddr);
797
798 sco->hl_link = hci_acl_open(unit, bdaddr);
799 KASSERT(sco->hl_link == acl);
800
801 sco->hl_sco = new;
802 new->sp_link = sco;
803
804 new->sp_mtu = unit->hci_max_sco_size;
805 return sco;
806 }
807
808 return NULL;
809 }
810
811 /*
812 * receive SCO packet, we only need to strip the header and send
813 * it to the right handler
814 */
815 void
816 hci_sco_recv(struct mbuf *m, struct hci_unit *unit)
817 {
818 struct hci_link *link;
819 hci_scodata_hdr_t hdr;
820 uint16_t handle;
821
822 KASSERT(m != NULL);
823 KASSERT(unit != NULL);
824
825 KASSERT(m->m_pkthdr.len >= sizeof(hdr));
826 m_copydata(m, 0, sizeof(hdr), &hdr);
827 m_adj(m, sizeof(hdr));
828
829 #ifdef DIAGNOSTIC
830 if (hdr.type != HCI_SCO_DATA_PKT) {
831 aprint_error_dev(unit->hci_dev, "bad SCO packet type\n");
832 goto bad;
833 }
834
835 if (m->m_pkthdr.len != hdr.length) {
836 aprint_error_dev(unit->hci_dev,
837 "bad SCO packet length (%d != %d)\n",
838 m->m_pkthdr.len, hdr.length);
839
840 goto bad;
841 }
842 #endif
843
844 hdr.con_handle = le16toh(hdr.con_handle);
845 handle = HCI_CON_HANDLE(hdr.con_handle);
846
847 link = hci_link_lookup_handle(unit, handle);
848 if (link == NULL || link->hl_type == HCI_LINK_ACL) {
849 DPRINTF("%s: dumping packet for unknown handle #%d\n",
850 device_xname(unit->hci_dev), handle);
851
852 goto bad;
853 }
854
855 (*link->hl_sco->sp_proto->input)(link->hl_sco->sp_upper, m);
856 return;
857
858 bad:
859 m_freem(m);
860 }
861
862 void
863 hci_sco_start(struct hci_link *link)
864 {
865 }
866
867 /*
868 * SCO packets have completed at the controller, so we can
869 * signal up to free the buffer space.
870 */
871 void
872 hci_sco_complete(struct hci_link *link, int num)
873 {
874
875 DPRINTFN(5, "handle #%d (num=%d)\n", link->hl_handle, num);
876 link->hl_sco->sp_pending--;
877 (*link->hl_sco->sp_proto->complete)(link->hl_sco->sp_upper, num);
878 }
879
880 /*******************************************************************************
881 *
882 * Generic HCI Connection alloc/free/lookup etc
883 */
884
885 struct hci_link *
886 hci_link_alloc(struct hci_unit *unit)
887 {
888 struct hci_link *link;
889
890 KASSERT(unit != NULL);
891
892 link = malloc(sizeof(struct hci_link), M_BLUETOOTH, M_NOWAIT | M_ZERO);
893 if (link == NULL)
894 return NULL;
895
896 link->hl_unit = unit;
897 link->hl_state = HCI_LINK_CLOSED;
898
899 /* init ACL portion */
900 callout_init(&link->hl_expire, 0);
901 callout_setfunc(&link->hl_expire, hci_acl_timeout, link);
902
903 TAILQ_INIT(&link->hl_txq); /* outgoing packets */
904 TAILQ_INIT(&link->hl_reqs); /* request queue */
905
906 link->hl_mtu = L2CAP_MTU_DEFAULT; /* L2CAP signal mtu */
907 link->hl_flush = L2CAP_FLUSH_TIMO_DEFAULT; /* flush timeout */
908
909 /* init SCO portion */
910 MBUFQ_INIT(&link->hl_data);
911
912 /* attach to unit */
913 TAILQ_INSERT_HEAD(&unit->hci_links, link, hl_next);
914 return link;
915 }
916
917 void
918 hci_link_free(struct hci_link *link, int err)
919 {
920 struct l2cap_req *req;
921 struct l2cap_pdu *pdu;
922 struct l2cap_channel *chan, *next;
923
924 KASSERT(link != NULL);
925
926 DPRINTF("#%d, type = %d, state = %d, refcnt = %d\n",
927 link->hl_handle, link->hl_type,
928 link->hl_state, link->hl_refcnt);
929
930 /* ACL reference count */
931 if (link->hl_refcnt > 0) {
932 next = LIST_FIRST(&l2cap_active_list);
933 while ((chan = next) != NULL) {
934 next = LIST_NEXT(chan, lc_ncid);
935 if (chan->lc_link == link)
936 l2cap_close(chan, err);
937 }
938 }
939 KASSERT(link->hl_refcnt == 0);
940
941 /* ACL L2CAP requests.. */
942 while ((req = TAILQ_FIRST(&link->hl_reqs)) != NULL)
943 l2cap_request_free(req);
944
945 KASSERT(TAILQ_EMPTY(&link->hl_reqs));
946
947 /* ACL outgoing data queue */
948 while ((pdu = TAILQ_FIRST(&link->hl_txq)) != NULL) {
949 TAILQ_REMOVE(&link->hl_txq, pdu, lp_next);
950 MBUFQ_DRAIN(&pdu->lp_data);
951 if (pdu->lp_pending)
952 link->hl_unit->hci_num_acl_pkts += pdu->lp_pending;
953
954 pool_put(&l2cap_pdu_pool, pdu);
955 }
956
957 KASSERT(TAILQ_EMPTY(&link->hl_txq));
958
959 /* ACL incoming data packet */
960 if (link->hl_rxp != NULL) {
961 m_freem(link->hl_rxp);
962 link->hl_rxp = NULL;
963 }
964
965 /* SCO master ACL link */
966 if (link->hl_link != NULL) {
967 hci_acl_close(link->hl_link, err);
968 link->hl_link = NULL;
969 }
970
971 /* SCO pcb */
972 if (link->hl_sco != NULL) {
973 struct sco_pcb *pcb;
974
975 pcb = link->hl_sco;
976 pcb->sp_link = NULL;
977 link->hl_sco = NULL;
978 (*pcb->sp_proto->disconnected)(pcb->sp_upper, err);
979 }
980
981 /* flush any SCO data */
982 MBUFQ_DRAIN(&link->hl_data);
983
984 /*
985 * Halt the callout - if its already running we cannot free the
986 * link structure but the timeout function will call us back in
987 * any case.
988 */
989 link->hl_state = HCI_LINK_CLOSED;
990 callout_stop(&link->hl_expire);
991 if (callout_invoking(&link->hl_expire))
992 return;
993
994 callout_destroy(&link->hl_expire);
995
996 /*
997 * If we made a note of clock offset, keep it in a memo
998 * to facilitate reconnections to this device
999 */
1000 if (link->hl_clock != 0) {
1001 struct hci_memo *memo;
1002
1003 memo = hci_memo_new(link->hl_unit, &link->hl_bdaddr);
1004 if (memo != NULL)
1005 memo->clock_offset = link->hl_clock;
1006 }
1007
1008 TAILQ_REMOVE(&link->hl_unit->hci_links, link, hl_next);
1009 free(link, M_BLUETOOTH);
1010 }
1011
1012 /*
1013 * Lookup HCI link by address and type. Note that for SCO links there may
1014 * be more than one link per address, so we only return links with no
1015 * handle (ie new links)
1016 */
1017 struct hci_link *
1018 hci_link_lookup_bdaddr(struct hci_unit *unit, bdaddr_t *bdaddr, uint16_t type)
1019 {
1020 struct hci_link *link;
1021
1022 KASSERT(unit != NULL);
1023 KASSERT(bdaddr != NULL);
1024
1025 TAILQ_FOREACH(link, &unit->hci_links, hl_next) {
1026 if (link->hl_type != type)
1027 continue;
1028
1029 if (type == HCI_LINK_SCO && link->hl_handle != 0)
1030 continue;
1031
1032 if (bdaddr_same(&link->hl_bdaddr, bdaddr))
1033 break;
1034 }
1035
1036 return link;
1037 }
1038
1039 struct hci_link *
1040 hci_link_lookup_handle(struct hci_unit *unit, uint16_t handle)
1041 {
1042 struct hci_link *link;
1043
1044 KASSERT(unit != NULL);
1045
1046 TAILQ_FOREACH(link, &unit->hci_links, hl_next) {
1047 if (handle == link->hl_handle)
1048 break;
1049 }
1050
1051 return link;
1052 }
1053