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