1 /* $OpenBSD: l2cap_lower.c,v 1.1 2007/05/30 03:42:53 uwe Exp $ */ 2 /* $NetBSD: l2cap_lower.c,v 1.6 2007/04/21 06:15:23 plunky Exp $ */ 3 /* $DragonFly: src/sys/netbt/l2cap_lower.c,v 1.1 2007/12/30 20:02:56 hasso Exp $ */ 4 5 /*- 6 * Copyright (c) 2005 Iain Hibbert. 7 * Copyright (c) 2006 Itronix Inc. 8 * All rights reserved. 9 * 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. 21 * 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. 33 */ 34 35 #include <sys/cdefs.h> 36 37 #include <sys/param.h> 38 #include <sys/kernel.h> 39 #include <sys/malloc.h> 40 #include <sys/mbuf.h> 41 #include <sys/proc.h> 42 #include <sys/queue.h> 43 #include <sys/systm.h> 44 #include <sys/endian.h> 45 46 #include <netbt/bluetooth.h> 47 #include <netbt/hci.h> 48 #include <netbt/l2cap.h> 49 50 /**************************************************************************** 51 * 52 * L2CAP Channel Lower Layer interface 53 */ 54 55 /* 56 * L2CAP channel is disconnected, could be: 57 * 58 * HCI layer received "Disconnect Complete" event for ACL link 59 * some Request timed out 60 * Config failed 61 * Other end reported invalid CID 62 * Normal disconnection 63 * Change link mode failed 64 */ 65 void 66 l2cap_close(struct l2cap_channel *chan, int err) 67 { 68 struct l2cap_pdu *pdu; 69 struct l2cap_req *req, *n; 70 71 if (chan->lc_state == L2CAP_CLOSED) 72 return; 73 74 /* 75 * Since any potential PDU could be half sent we just let it go, 76 * but disassociate ourselves from it as links deal with ownerless 77 * PDU's in any case. We could try harder to flush unsent packets 78 * but maybe its better to leave them in the queue? 79 */ 80 TAILQ_FOREACH(pdu, &chan->lc_link->hl_txq, lp_next) { 81 if (pdu->lp_chan == chan) 82 pdu->lp_chan = NULL; 83 } 84 85 /* 86 * and clear any outstanding requests.. 87 */ 88 req = TAILQ_FIRST(&chan->lc_link->hl_reqs); 89 while (req != NULL) { 90 n = TAILQ_NEXT(req, lr_next); 91 if (req->lr_chan == chan) 92 l2cap_request_free(req); 93 94 req = n; 95 } 96 97 chan->lc_pending = 0; 98 chan->lc_state = L2CAP_CLOSED; 99 hci_acl_close(chan->lc_link, err); 100 chan->lc_link = NULL; 101 102 (*chan->lc_proto->disconnected)(chan->lc_upper, err); 103 } 104 105 /* 106 * Process incoming L2CAP frame from ACL link. We take off the B-Frame 107 * header (which is present in all packets), verify the data length 108 * and distribute the rest of the frame to the relevant channel 109 * handler. 110 */ 111 void 112 l2cap_recv_frame(struct mbuf *m, struct hci_link *link) 113 { 114 struct l2cap_channel *chan; 115 l2cap_hdr_t hdr; 116 117 m_copydata(m, 0, sizeof(hdr), (caddr_t)&hdr); 118 m_adj(m, sizeof(hdr)); 119 120 hdr.length = letoh16(hdr.length); 121 hdr.dcid = letoh16(hdr.dcid); 122 123 DPRINTFN(5, "(%s) received packet (%d bytes)\n", 124 link->hl_unit->hci_devname, hdr.length); 125 126 if (hdr.length != m->m_pkthdr.len) 127 goto failed; 128 129 if (hdr.dcid == L2CAP_SIGNAL_CID) { 130 l2cap_recv_signal(m, link); 131 return; 132 } 133 134 if (hdr.dcid == L2CAP_CLT_CID) { 135 m_freem(m); /* TODO */ 136 return; 137 } 138 139 chan = l2cap_cid_lookup(hdr.dcid); 140 if (chan != NULL && chan->lc_link == link 141 && chan->lc_state == L2CAP_OPEN) { 142 (*chan->lc_proto->input)(chan->lc_upper, m); 143 return; 144 } 145 146 DPRINTF("(%s) dropping %d L2CAP data bytes for unknown CID #%d\n", 147 link->hl_unit->hci_devname, hdr.length, hdr.dcid); 148 149 failed: 150 m_freem(m); 151 } 152 153 /* 154 * Start another L2CAP packet on its way. This is called from l2cap_send 155 * (when no PDU is pending) and hci_acl_start (when PDU has been placed on 156 * device queue). Thus we can have more than one PDU waiting at the device 157 * if space is available but no single channel will hog the link. 158 */ 159 int 160 l2cap_start(struct l2cap_channel *chan) 161 { 162 struct mbuf *m; 163 int err = 0; 164 165 if (chan->lc_state != L2CAP_OPEN) 166 return 0; 167 168 if (IF_QEMPTY(&chan->lc_txq)) { 169 DPRINTFN(5, "no data, pending = %d\n", chan->lc_pending); 170 /* 171 * If we are just waiting for the queue to flush 172 * and it has, we may disconnect.. 173 */ 174 if (chan->lc_flags & L2CAP_SHUTDOWN 175 && chan->lc_pending == 0) { 176 chan->lc_state = L2CAP_WAIT_DISCONNECT; 177 err = l2cap_send_disconnect_req(chan); 178 if (err) 179 l2cap_close(chan, err); 180 } 181 182 return err; 183 } 184 185 /* 186 * We could check QoS/RFC mode here and optionally not send 187 * the packet if we are not ready for any reason 188 * 189 * Also to support flush timeout then we might want to start 190 * the timer going? (would need to keep some kind of record 191 * of packets sent, possibly change it so that we allocate 192 * the l2cap_pdu and fragment the packet, then hand it down 193 * and get it back when its completed). Hm. 194 */ 195 196 IF_DEQUEUE(&chan->lc_txq, m); 197 198 KKASSERT(chan->lc_link != NULL); 199 KKASSERT(m != NULL); 200 201 DPRINTFN(5, "CID #%d sending packet (%d bytes)\n", 202 chan->lc_lcid, m->m_pkthdr.len); 203 204 chan->lc_pending++; 205 return hci_acl_send(m, chan->lc_link, chan); 206 } 207