hci_link.c revision 1.1.4.2 1 1.1.4.2 chap /* $NetBSD: hci_link.c,v 1.1.4.2 2006/06/22 03:39:50 chap Exp $ */
2 1.1.4.2 chap
3 1.1.4.2 chap /*-
4 1.1.4.2 chap * Copyright (c) 2005 Iain Hibbert.
5 1.1.4.2 chap * Copyright (c) 2006 Itronix Inc.
6 1.1.4.2 chap * All rights reserved.
7 1.1.4.2 chap *
8 1.1.4.2 chap * Redistribution and use in source and binary forms, with or without
9 1.1.4.2 chap * modification, are permitted provided that the following conditions
10 1.1.4.2 chap * are met:
11 1.1.4.2 chap * 1. Redistributions of source code must retain the above copyright
12 1.1.4.2 chap * notice, this list of conditions and the following disclaimer.
13 1.1.4.2 chap * 2. Redistributions in binary form must reproduce the above copyright
14 1.1.4.2 chap * notice, this list of conditions and the following disclaimer in the
15 1.1.4.2 chap * documentation and/or other materials provided with the distribution.
16 1.1.4.2 chap * 3. The name of Itronix Inc. may not be used to endorse
17 1.1.4.2 chap * or promote products derived from this software without specific
18 1.1.4.2 chap * prior written permission.
19 1.1.4.2 chap *
20 1.1.4.2 chap * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21 1.1.4.2 chap * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1.4.2 chap * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1.4.2 chap * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24 1.1.4.2 chap * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 1.1.4.2 chap * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 1.1.4.2 chap * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 1.1.4.2 chap * ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1.4.2 chap * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1.4.2 chap * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1.4.2 chap * POSSIBILITY OF SUCH DAMAGE.
31 1.1.4.2 chap */
32 1.1.4.2 chap
33 1.1.4.2 chap #include <sys/cdefs.h>
34 1.1.4.2 chap __KERNEL_RCSID(0, "$NetBSD: hci_link.c,v 1.1.4.2 2006/06/22 03:39:50 chap Exp $");
35 1.1.4.2 chap
36 1.1.4.2 chap #include <sys/param.h>
37 1.1.4.2 chap #include <sys/kernel.h>
38 1.1.4.2 chap #include <sys/malloc.h>
39 1.1.4.2 chap #include <sys/mbuf.h>
40 1.1.4.2 chap #include <sys/proc.h>
41 1.1.4.2 chap #include <sys/queue.h>
42 1.1.4.2 chap #include <sys/systm.h>
43 1.1.4.2 chap
44 1.1.4.2 chap #include <netbt/bluetooth.h>
45 1.1.4.2 chap #include <netbt/hci.h>
46 1.1.4.2 chap #include <netbt/sco.h>
47 1.1.4.2 chap #include <netbt/l2cap.h>
48 1.1.4.2 chap
49 1.1.4.2 chap /*******************************************************************************
50 1.1.4.2 chap *
51 1.1.4.2 chap * HCI ACL Connections
52 1.1.4.2 chap */
53 1.1.4.2 chap
54 1.1.4.2 chap /*
55 1.1.4.2 chap * Automatically expire unused ACL connections after this number of
56 1.1.4.2 chap * seconds (if zero, do not expire unused connections) [sysctl]
57 1.1.4.2 chap */
58 1.1.4.2 chap int hci_acl_expiry = 10; /* seconds */
59 1.1.4.2 chap
60 1.1.4.2 chap /*
61 1.1.4.2 chap * hci_acl_open(unit, bdaddr)
62 1.1.4.2 chap *
63 1.1.4.2 chap * open ACL connection to remote bdaddr. Only one ACL connection is permitted
64 1.1.4.2 chap * between any two Bluetooth devices, so we look for an existing one before
65 1.1.4.2 chap * trying to start a new one.
66 1.1.4.2 chap */
67 1.1.4.2 chap struct hci_link *
68 1.1.4.2 chap hci_acl_open(struct hci_unit *unit, bdaddr_t *bdaddr)
69 1.1.4.2 chap {
70 1.1.4.2 chap struct hci_link *link;
71 1.1.4.2 chap hci_create_con_cp cp;
72 1.1.4.2 chap int err;
73 1.1.4.2 chap
74 1.1.4.2 chap KASSERT(unit);
75 1.1.4.2 chap KASSERT(bdaddr);
76 1.1.4.2 chap
77 1.1.4.2 chap link = hci_link_lookup_bdaddr(unit, bdaddr, HCI_LINK_ACL);
78 1.1.4.2 chap if (link == NULL) {
79 1.1.4.2 chap link = hci_link_alloc(unit);
80 1.1.4.2 chap if (link == NULL)
81 1.1.4.2 chap return NULL;
82 1.1.4.2 chap
83 1.1.4.2 chap link->hl_type = HCI_LINK_ACL;
84 1.1.4.2 chap bdaddr_copy(&link->hl_bdaddr, bdaddr);
85 1.1.4.2 chap }
86 1.1.4.2 chap
87 1.1.4.2 chap switch(link->hl_state) {
88 1.1.4.2 chap case HCI_LINK_CLOSED:
89 1.1.4.2 chap /*
90 1.1.4.2 chap * open connection to remote device
91 1.1.4.2 chap */
92 1.1.4.2 chap memset(&cp, 0, sizeof(cp));
93 1.1.4.2 chap bdaddr_copy(&cp.bdaddr, bdaddr);
94 1.1.4.2 chap cp.pkt_type = htole16(unit->hci_packet_type);
95 1.1.4.2 chap if (unit->hci_link_policy & HCI_LINK_POLICY_ENABLE_ROLE_SWITCH)
96 1.1.4.2 chap cp.accept_role_switch = 1;
97 1.1.4.2 chap
98 1.1.4.2 chap err = hci_send_cmd(unit, HCI_CMD_CREATE_CON, &cp, sizeof(cp));
99 1.1.4.2 chap if (err) {
100 1.1.4.2 chap hci_link_free(link, err);
101 1.1.4.2 chap return NULL;
102 1.1.4.2 chap }
103 1.1.4.2 chap
104 1.1.4.2 chap link->hl_state = HCI_LINK_WAIT_CONNECT;
105 1.1.4.2 chap break;
106 1.1.4.2 chap
107 1.1.4.2 chap case HCI_LINK_WAIT_CONNECT:
108 1.1.4.2 chap /*
109 1.1.4.2 chap * somebody else already trying to connect, we just
110 1.1.4.2 chap * sit on the bench with them..
111 1.1.4.2 chap */
112 1.1.4.2 chap break;
113 1.1.4.2 chap
114 1.1.4.2 chap case HCI_LINK_OPEN:
115 1.1.4.2 chap /*
116 1.1.4.2 chap * If already open, halt any expiry timeouts. We dont need
117 1.1.4.2 chap * to care about already invoking timeouts since refcnt >0
118 1.1.4.2 chap * will keep the link alive.
119 1.1.4.2 chap */
120 1.1.4.2 chap callout_stop(&link->hl_expire);
121 1.1.4.2 chap break;
122 1.1.4.2 chap
123 1.1.4.2 chap default:
124 1.1.4.2 chap UNKNOWN(link->hl_state);
125 1.1.4.2 chap return NULL;
126 1.1.4.2 chap }
127 1.1.4.2 chap
128 1.1.4.2 chap /* open */
129 1.1.4.2 chap link->hl_refcnt++;
130 1.1.4.2 chap
131 1.1.4.2 chap return link;
132 1.1.4.2 chap }
133 1.1.4.2 chap
134 1.1.4.2 chap /*
135 1.1.4.2 chap * Close ACL connection. When there are no more references to this link,
136 1.1.4.2 chap * we can either close it down or schedule a delayed closedown.
137 1.1.4.2 chap */
138 1.1.4.2 chap void
139 1.1.4.2 chap hci_acl_close(struct hci_link *link, int err)
140 1.1.4.2 chap {
141 1.1.4.2 chap
142 1.1.4.2 chap KASSERT(link);
143 1.1.4.2 chap
144 1.1.4.2 chap if (--link->hl_refcnt == 0) {
145 1.1.4.2 chap if (link->hl_state == HCI_LINK_CLOSED)
146 1.1.4.2 chap hci_link_free(link, err);
147 1.1.4.2 chap else if (hci_acl_expiry > 0)
148 1.1.4.2 chap callout_schedule(&link->hl_expire, hci_acl_expiry * hz);
149 1.1.4.2 chap }
150 1.1.4.2 chap }
151 1.1.4.2 chap
152 1.1.4.2 chap /*
153 1.1.4.2 chap * Incoming ACL connection. For now, we accept all connections but it
154 1.1.4.2 chap * would be better to check the L2CAP listen list and only accept when
155 1.1.4.2 chap * there is a listener available.
156 1.1.4.2 chap */
157 1.1.4.2 chap struct hci_link *
158 1.1.4.2 chap hci_acl_newconn(struct hci_unit *unit, bdaddr_t *bdaddr)
159 1.1.4.2 chap {
160 1.1.4.2 chap struct hci_link *link;
161 1.1.4.2 chap
162 1.1.4.2 chap link = hci_link_alloc(unit);
163 1.1.4.2 chap if (link != NULL) {
164 1.1.4.2 chap link->hl_state = HCI_LINK_WAIT_CONNECT;
165 1.1.4.2 chap link->hl_type = HCI_LINK_ACL;
166 1.1.4.2 chap bdaddr_copy(&link->hl_bdaddr, bdaddr);
167 1.1.4.2 chap
168 1.1.4.2 chap if (hci_acl_expiry > 0)
169 1.1.4.2 chap callout_schedule(&link->hl_expire, hci_acl_expiry * hz);
170 1.1.4.2 chap }
171 1.1.4.2 chap
172 1.1.4.2 chap return link;
173 1.1.4.2 chap }
174 1.1.4.2 chap
175 1.1.4.2 chap void
176 1.1.4.2 chap hci_acl_timeout(void *arg)
177 1.1.4.2 chap {
178 1.1.4.2 chap struct hci_link *link = arg;
179 1.1.4.2 chap hci_discon_cp cp;
180 1.1.4.2 chap int s, err;
181 1.1.4.2 chap
182 1.1.4.2 chap s = splsoftnet();
183 1.1.4.2 chap callout_ack(&link->hl_expire);
184 1.1.4.2 chap
185 1.1.4.2 chap if (link->hl_refcnt > 0)
186 1.1.4.2 chap goto out;
187 1.1.4.2 chap
188 1.1.4.2 chap DPRINTF("link #%d expired\n", link->hl_handle);
189 1.1.4.2 chap
190 1.1.4.2 chap switch (link->hl_state) {
191 1.1.4.2 chap case HCI_LINK_CLOSED:
192 1.1.4.2 chap case HCI_LINK_WAIT_CONNECT:
193 1.1.4.2 chap hci_link_free(link, ECONNRESET);
194 1.1.4.2 chap break;
195 1.1.4.2 chap
196 1.1.4.2 chap case HCI_LINK_OPEN:
197 1.1.4.2 chap cp.con_handle = htole16(link->hl_handle);
198 1.1.4.2 chap cp.reason = 0x13; /* "Remote User Terminated Connection" */
199 1.1.4.2 chap
200 1.1.4.2 chap err = hci_send_cmd(link->hl_unit, HCI_CMD_DISCONNECT,
201 1.1.4.2 chap &cp, sizeof(cp));
202 1.1.4.2 chap
203 1.1.4.2 chap if (err)
204 1.1.4.2 chap DPRINTF("error %d sending HCI_CMD_DISCONNECT\n",
205 1.1.4.2 chap err);
206 1.1.4.2 chap
207 1.1.4.2 chap break;
208 1.1.4.2 chap
209 1.1.4.2 chap default:
210 1.1.4.2 chap UNKNOWN(link->hl_state);
211 1.1.4.2 chap break;
212 1.1.4.2 chap }
213 1.1.4.2 chap
214 1.1.4.2 chap out:
215 1.1.4.2 chap splx(s);
216 1.1.4.2 chap }
217 1.1.4.2 chap
218 1.1.4.2 chap /*
219 1.1.4.2 chap * Receive ACL Data
220 1.1.4.2 chap *
221 1.1.4.2 chap * we accumulate packet fragments on the hci_link structure
222 1.1.4.2 chap * until a full L2CAP frame is ready, then send it on.
223 1.1.4.2 chap */
224 1.1.4.2 chap void
225 1.1.4.2 chap hci_acl_recv(struct mbuf *m, struct hci_unit *unit)
226 1.1.4.2 chap {
227 1.1.4.2 chap struct hci_link *link;
228 1.1.4.2 chap hci_acldata_hdr_t hdr;
229 1.1.4.2 chap uint16_t handle, want;
230 1.1.4.2 chap int pb, got;
231 1.1.4.2 chap
232 1.1.4.2 chap KASSERT(m);
233 1.1.4.2 chap KASSERT(unit);
234 1.1.4.2 chap
235 1.1.4.2 chap KASSERT(m->m_pkthdr.len >= sizeof(hdr));
236 1.1.4.2 chap m_copydata(m, 0, sizeof(hdr), &hdr);
237 1.1.4.2 chap m_adj(m, sizeof(hdr));
238 1.1.4.2 chap
239 1.1.4.2 chap #ifdef DIAGNOSTIC
240 1.1.4.2 chap if (hdr.type != HCI_ACL_DATA_PKT) {
241 1.1.4.2 chap printf("%s: bad ACL packet type\n", unit->hci_devname);
242 1.1.4.2 chap goto bad;
243 1.1.4.2 chap }
244 1.1.4.2 chap
245 1.1.4.2 chap if (m->m_pkthdr.len != le16toh(hdr.length)) {
246 1.1.4.2 chap printf("%s: bad ACL packet length\n", unit->hci_devname);
247 1.1.4.2 chap goto bad;
248 1.1.4.2 chap }
249 1.1.4.2 chap #endif
250 1.1.4.2 chap
251 1.1.4.2 chap hdr.length = le16toh(hdr.length);
252 1.1.4.2 chap hdr.con_handle = le16toh(hdr.con_handle);
253 1.1.4.2 chap handle = HCI_CON_HANDLE(hdr.con_handle);
254 1.1.4.2 chap pb = HCI_PB_FLAG(hdr.con_handle);
255 1.1.4.2 chap
256 1.1.4.2 chap link = hci_link_lookup_handle(unit, handle);
257 1.1.4.2 chap if (link == NULL) {
258 1.1.4.2 chap hci_discon_cp cp;
259 1.1.4.2 chap
260 1.1.4.2 chap DPRINTF("%s: dumping packet for unknown handle #%d\n",
261 1.1.4.2 chap unit->hci_devname, handle);
262 1.1.4.2 chap
263 1.1.4.2 chap /*
264 1.1.4.2 chap * There is no way to find out what this connection handle is
265 1.1.4.2 chap * for, just get rid of it. This may happen, if a USB dongle
266 1.1.4.2 chap * is plugged into a self powered hub and does not reset when
267 1.1.4.2 chap * the system is shut down.
268 1.1.4.2 chap */
269 1.1.4.2 chap cp.con_handle = htole16(handle);
270 1.1.4.2 chap cp.reason = 0x13; /* "Remote User Terminated Connection" */
271 1.1.4.2 chap hci_send_cmd(unit, HCI_CMD_DISCONNECT, &cp, sizeof(cp));
272 1.1.4.2 chap goto bad;
273 1.1.4.2 chap }
274 1.1.4.2 chap
275 1.1.4.2 chap switch (pb) {
276 1.1.4.2 chap case HCI_PACKET_START:
277 1.1.4.2 chap if (link->hl_rxp != NULL)
278 1.1.4.2 chap printf("%s: dropped incomplete ACL packet\n",
279 1.1.4.2 chap unit->hci_devname);
280 1.1.4.2 chap
281 1.1.4.2 chap if (m->m_pkthdr.len < sizeof(l2cap_hdr_t)) {
282 1.1.4.2 chap printf("%s: short ACL packet\n",
283 1.1.4.2 chap unit->hci_devname);
284 1.1.4.2 chap
285 1.1.4.2 chap goto bad;
286 1.1.4.2 chap }
287 1.1.4.2 chap
288 1.1.4.2 chap link->hl_rxp = m;
289 1.1.4.2 chap got = m->m_pkthdr.len;
290 1.1.4.2 chap break;
291 1.1.4.2 chap
292 1.1.4.2 chap case HCI_PACKET_FRAGMENT:
293 1.1.4.2 chap if (link->hl_rxp == NULL) {
294 1.1.4.2 chap printf("%s: unexpected packet fragment\n",
295 1.1.4.2 chap unit->hci_devname);
296 1.1.4.2 chap
297 1.1.4.2 chap goto bad;
298 1.1.4.2 chap }
299 1.1.4.2 chap
300 1.1.4.2 chap got = m->m_pkthdr.len + link->hl_rxp->m_pkthdr.len;
301 1.1.4.2 chap m_cat(link->hl_rxp, m);
302 1.1.4.2 chap m = link->hl_rxp;
303 1.1.4.2 chap m->m_pkthdr.len = got;
304 1.1.4.2 chap break;
305 1.1.4.2 chap
306 1.1.4.2 chap default:
307 1.1.4.2 chap printf("%s: unknown packet type\n",
308 1.1.4.2 chap unit->hci_devname);
309 1.1.4.2 chap
310 1.1.4.2 chap goto bad;
311 1.1.4.2 chap }
312 1.1.4.2 chap
313 1.1.4.2 chap m_copydata(m, 0, sizeof(want), &want);
314 1.1.4.2 chap want = le16toh(want) + sizeof(l2cap_hdr_t) - got;
315 1.1.4.2 chap
316 1.1.4.2 chap if (want > 0)
317 1.1.4.2 chap return;
318 1.1.4.2 chap
319 1.1.4.2 chap link->hl_rxp = NULL;
320 1.1.4.2 chap
321 1.1.4.2 chap if (want == 0) {
322 1.1.4.2 chap l2cap_recv_frame(m, link);
323 1.1.4.2 chap return;
324 1.1.4.2 chap }
325 1.1.4.2 chap
326 1.1.4.2 chap bad:
327 1.1.4.2 chap m_freem(m);
328 1.1.4.2 chap }
329 1.1.4.2 chap
330 1.1.4.2 chap /*
331 1.1.4.2 chap * Send ACL data on link
332 1.1.4.2 chap *
333 1.1.4.2 chap * We must fragment packets into chunks of less than unit->hci_max_acl_size and
334 1.1.4.2 chap * prepend a relevant ACL header to each fragment. We keep a PDU structure
335 1.1.4.2 chap * attached to the link, so that completed fragments can be marked off and
336 1.1.4.2 chap * more data requested from above once the PDU is sent.
337 1.1.4.2 chap */
338 1.1.4.2 chap int
339 1.1.4.2 chap hci_acl_send(struct mbuf *m, struct hci_link *link,
340 1.1.4.2 chap struct l2cap_channel *chan)
341 1.1.4.2 chap {
342 1.1.4.2 chap struct l2cap_pdu *pdu;
343 1.1.4.2 chap struct mbuf *n = NULL;
344 1.1.4.2 chap int plen, mlen, num = 0;
345 1.1.4.2 chap
346 1.1.4.2 chap KASSERT(link);
347 1.1.4.2 chap KASSERT(m);
348 1.1.4.2 chap KASSERT(m->m_flags & M_PKTHDR);
349 1.1.4.2 chap KASSERT(m->m_pkthdr.len > 0);
350 1.1.4.2 chap
351 1.1.4.2 chap if (link->hl_state == HCI_LINK_CLOSED) {
352 1.1.4.2 chap m_freem(m);
353 1.1.4.2 chap return ENETDOWN;
354 1.1.4.2 chap }
355 1.1.4.2 chap
356 1.1.4.2 chap pdu = pool_get(&l2cap_pdu_pool, PR_NOWAIT);
357 1.1.4.2 chap if (pdu == NULL)
358 1.1.4.2 chap goto nomem;
359 1.1.4.2 chap
360 1.1.4.2 chap pdu->lp_chan = chan;
361 1.1.4.2 chap pdu->lp_pending = 0;
362 1.1.4.2 chap MBUFQ_INIT(&pdu->lp_data);
363 1.1.4.2 chap
364 1.1.4.2 chap plen = m->m_pkthdr.len;
365 1.1.4.2 chap mlen = link->hl_unit->hci_max_acl_size;
366 1.1.4.2 chap
367 1.1.4.2 chap DPRINTFN(5, "%s: handle #%d, plen = %d, max = %d\n",
368 1.1.4.2 chap link->hl_unit->hci_devname, link->hl_handle, plen, mlen);
369 1.1.4.2 chap
370 1.1.4.2 chap while (plen > 0) {
371 1.1.4.2 chap if (plen > mlen) {
372 1.1.4.2 chap n = m_split(m, mlen, M_DONTWAIT);
373 1.1.4.2 chap if (n == NULL)
374 1.1.4.2 chap goto nomem;
375 1.1.4.2 chap } else {
376 1.1.4.2 chap mlen = plen;
377 1.1.4.2 chap }
378 1.1.4.2 chap
379 1.1.4.2 chap if (num++ == 0)
380 1.1.4.2 chap m->m_flags |= M_PROTO1; /* tag first fragment */
381 1.1.4.2 chap
382 1.1.4.2 chap DPRINTFN(10, "chunk of %d (plen = %d) bytes\n", mlen, plen);
383 1.1.4.2 chap MBUFQ_ENQUEUE(&pdu->lp_data, m);
384 1.1.4.2 chap m = n;
385 1.1.4.2 chap plen -= mlen;
386 1.1.4.2 chap }
387 1.1.4.2 chap
388 1.1.4.2 chap TAILQ_INSERT_TAIL(&link->hl_txq, pdu, lp_next);
389 1.1.4.2 chap link->hl_txqlen += num;
390 1.1.4.2 chap
391 1.1.4.2 chap hci_acl_start(link);
392 1.1.4.2 chap
393 1.1.4.2 chap return 0;
394 1.1.4.2 chap
395 1.1.4.2 chap nomem:
396 1.1.4.2 chap if (m) m_freem(m);
397 1.1.4.2 chap if (n) m_freem(n);
398 1.1.4.2 chap if (pdu) {
399 1.1.4.2 chap MBUFQ_DRAIN(&pdu->lp_data);
400 1.1.4.2 chap pool_put(&l2cap_pdu_pool, pdu);
401 1.1.4.2 chap }
402 1.1.4.2 chap
403 1.1.4.2 chap return ENOMEM;
404 1.1.4.2 chap }
405 1.1.4.2 chap
406 1.1.4.2 chap /*
407 1.1.4.2 chap * Start sending ACL data on link.
408 1.1.4.2 chap *
409 1.1.4.2 chap * We may use all the available packet slots. The reason that we add
410 1.1.4.2 chap * the ACL encapsulation here rather than in hci_acl_send() is that L2CAP
411 1.1.4.2 chap * signal packets may be queued before the handle is given to us..
412 1.1.4.2 chap *
413 1.1.4.2 chap * this is called from hci_acl_send() above, and the event processing
414 1.1.4.2 chap * code (for CON_COMPL and NUM_COMPL_PKTS)
415 1.1.4.2 chap */
416 1.1.4.2 chap void
417 1.1.4.2 chap hci_acl_start(struct hci_link *link)
418 1.1.4.2 chap {
419 1.1.4.2 chap struct hci_unit *unit;
420 1.1.4.2 chap hci_acldata_hdr_t *hdr;
421 1.1.4.2 chap struct l2cap_pdu *pdu;
422 1.1.4.2 chap struct mbuf *m;
423 1.1.4.2 chap uint16_t handle;
424 1.1.4.2 chap
425 1.1.4.2 chap KASSERT(link);
426 1.1.4.2 chap
427 1.1.4.2 chap unit = link->hl_unit;
428 1.1.4.2 chap KASSERT(unit);
429 1.1.4.2 chap
430 1.1.4.2 chap /* this is mainly to block ourselves (below) */
431 1.1.4.2 chap if (link->hl_state != HCI_LINK_OPEN)
432 1.1.4.2 chap return;
433 1.1.4.2 chap
434 1.1.4.2 chap if (link->hl_txqlen == 0 || unit->hci_num_acl_pkts == 0)
435 1.1.4.2 chap return;
436 1.1.4.2 chap
437 1.1.4.2 chap /* find first PDU with data to send */
438 1.1.4.2 chap pdu = TAILQ_FIRST(&link->hl_txq);
439 1.1.4.2 chap for (;;) {
440 1.1.4.2 chap if (pdu == NULL)
441 1.1.4.2 chap return;
442 1.1.4.2 chap
443 1.1.4.2 chap if (MBUFQ_FIRST(&pdu->lp_data) != NULL)
444 1.1.4.2 chap break;
445 1.1.4.2 chap
446 1.1.4.2 chap pdu = TAILQ_NEXT(pdu, lp_next);
447 1.1.4.2 chap }
448 1.1.4.2 chap
449 1.1.4.2 chap while (unit->hci_num_acl_pkts > 0) {
450 1.1.4.2 chap MBUFQ_DEQUEUE(&pdu->lp_data, m);
451 1.1.4.2 chap KASSERT(m != NULL);
452 1.1.4.2 chap
453 1.1.4.2 chap if (m->m_flags & M_PROTO1)
454 1.1.4.2 chap handle = HCI_MK_CON_HANDLE(link->hl_handle,
455 1.1.4.2 chap HCI_PACKET_START, 0);
456 1.1.4.2 chap else
457 1.1.4.2 chap handle = HCI_MK_CON_HANDLE(link->hl_handle,
458 1.1.4.2 chap HCI_PACKET_FRAGMENT, 0);
459 1.1.4.2 chap
460 1.1.4.2 chap M_PREPEND(m, sizeof(*hdr), M_DONTWAIT);
461 1.1.4.2 chap if (m == NULL)
462 1.1.4.2 chap break;
463 1.1.4.2 chap
464 1.1.4.2 chap hdr = mtod(m, hci_acldata_hdr_t *);
465 1.1.4.2 chap hdr->type = HCI_ACL_DATA_PKT;
466 1.1.4.2 chap hdr->con_handle = htole16(handle);
467 1.1.4.2 chap hdr->length = htole16(m->m_pkthdr.len - sizeof(*hdr));
468 1.1.4.2 chap
469 1.1.4.2 chap link->hl_txqlen--;
470 1.1.4.2 chap pdu->lp_pending++;
471 1.1.4.2 chap
472 1.1.4.2 chap hci_output_acl(unit, m);
473 1.1.4.2 chap
474 1.1.4.2 chap if (MBUFQ_FIRST(&pdu->lp_data) == NULL) {
475 1.1.4.2 chap if (pdu->lp_chan) {
476 1.1.4.2 chap /*
477 1.1.4.2 chap * This should enable streaming of PDUs - when
478 1.1.4.2 chap * we have placed all the fragments on the acl
479 1.1.4.2 chap * output queue, we trigger the L2CAP layer to
480 1.1.4.2 chap * send us down one more. Use a false state so
481 1.1.4.2 chap * we dont run into ourselves coming back from
482 1.1.4.2 chap * the future..
483 1.1.4.2 chap */
484 1.1.4.2 chap link->hl_state = HCI_LINK_BLOCK;
485 1.1.4.2 chap l2cap_start(pdu->lp_chan);
486 1.1.4.2 chap link->hl_state = HCI_LINK_OPEN;
487 1.1.4.2 chap }
488 1.1.4.2 chap
489 1.1.4.2 chap pdu = TAILQ_NEXT(pdu, lp_next);
490 1.1.4.2 chap if (pdu == NULL)
491 1.1.4.2 chap break;
492 1.1.4.2 chap }
493 1.1.4.2 chap }
494 1.1.4.2 chap
495 1.1.4.2 chap /*
496 1.1.4.2 chap * We had our turn now, move to the back of the queue to let
497 1.1.4.2 chap * other links have a go at the output buffers..
498 1.1.4.2 chap */
499 1.1.4.2 chap if (TAILQ_NEXT(link, hl_next)) {
500 1.1.4.2 chap TAILQ_REMOVE(&unit->hci_links, link, hl_next);
501 1.1.4.2 chap TAILQ_INSERT_TAIL(&unit->hci_links, link, hl_next);
502 1.1.4.2 chap }
503 1.1.4.2 chap }
504 1.1.4.2 chap
505 1.1.4.2 chap /*
506 1.1.4.2 chap * Confirm ACL packets cleared from Controller buffers. We scan our PDU
507 1.1.4.2 chap * list to clear pending fragments and signal upstream for more data
508 1.1.4.2 chap * when a PDU is complete.
509 1.1.4.2 chap */
510 1.1.4.2 chap void
511 1.1.4.2 chap hci_acl_complete(struct hci_link *link, int num)
512 1.1.4.2 chap {
513 1.1.4.2 chap struct l2cap_pdu *pdu;
514 1.1.4.2 chap struct l2cap_channel *chan;
515 1.1.4.2 chap
516 1.1.4.2 chap DPRINTFN(5, "handle #%d (%d)\n", link->hl_handle, num);
517 1.1.4.2 chap
518 1.1.4.2 chap while (num > 0) {
519 1.1.4.2 chap pdu = TAILQ_FIRST(&link->hl_txq);
520 1.1.4.2 chap if (pdu == NULL) {
521 1.1.4.2 chap printf("%s: %d packets completed on handle #%x "
522 1.1.4.2 chap "but none pending!\n",
523 1.1.4.2 chap link->hl_unit->hci_devname, num,
524 1.1.4.2 chap link->hl_handle);
525 1.1.4.2 chap return;
526 1.1.4.2 chap }
527 1.1.4.2 chap
528 1.1.4.2 chap if (num >= pdu->lp_pending) {
529 1.1.4.2 chap num -= pdu->lp_pending;
530 1.1.4.2 chap pdu->lp_pending = 0;
531 1.1.4.2 chap
532 1.1.4.2 chap if (MBUFQ_FIRST(&pdu->lp_data) == NULL) {
533 1.1.4.2 chap TAILQ_REMOVE(&link->hl_txq, pdu, lp_next);
534 1.1.4.2 chap chan = pdu->lp_chan;
535 1.1.4.2 chap if (chan != NULL) {
536 1.1.4.2 chap chan->lc_pending--;
537 1.1.4.2 chap (*chan->lc_proto->complete)
538 1.1.4.2 chap (chan->lc_upper, 1);
539 1.1.4.2 chap
540 1.1.4.2 chap if (chan->lc_pending == 0)
541 1.1.4.2 chap l2cap_start(chan);
542 1.1.4.2 chap }
543 1.1.4.2 chap
544 1.1.4.2 chap pool_put(&l2cap_pdu_pool, pdu);
545 1.1.4.2 chap }
546 1.1.4.2 chap } else {
547 1.1.4.2 chap pdu->lp_pending -= num;
548 1.1.4.2 chap num = 0;
549 1.1.4.2 chap }
550 1.1.4.2 chap }
551 1.1.4.2 chap }
552 1.1.4.2 chap
553 1.1.4.2 chap /*******************************************************************************
554 1.1.4.2 chap *
555 1.1.4.2 chap * HCI SCO Connections
556 1.1.4.2 chap */
557 1.1.4.2 chap
558 1.1.4.2 chap /*
559 1.1.4.2 chap * Incoming SCO Connection. Not yet implemented
560 1.1.4.2 chap */
561 1.1.4.2 chap struct hci_link *
562 1.1.4.2 chap hci_sco_newconn(struct hci_unit *unit, bdaddr_t *bdaddr)
563 1.1.4.2 chap {
564 1.1.4.2 chap
565 1.1.4.2 chap return NULL;
566 1.1.4.2 chap }
567 1.1.4.2 chap
568 1.1.4.2 chap /*
569 1.1.4.2 chap * receive SCO packet, we only need to strip the header and send
570 1.1.4.2 chap * it to the right handler
571 1.1.4.2 chap */
572 1.1.4.2 chap void
573 1.1.4.2 chap hci_sco_recv(struct mbuf *m, struct hci_unit *unit)
574 1.1.4.2 chap {
575 1.1.4.2 chap struct hci_link *link;
576 1.1.4.2 chap hci_scodata_hdr_t hdr;
577 1.1.4.2 chap uint16_t handle;
578 1.1.4.2 chap
579 1.1.4.2 chap KASSERT(m);
580 1.1.4.2 chap KASSERT(unit);
581 1.1.4.2 chap
582 1.1.4.2 chap KASSERT(m->m_pkthdr.len >= sizeof(hdr));
583 1.1.4.2 chap m_copydata(m, 0, sizeof(hdr), &hdr);
584 1.1.4.2 chap m_adj(m, sizeof(hdr));
585 1.1.4.2 chap
586 1.1.4.2 chap #ifdef DIAGNOSTIC
587 1.1.4.2 chap if (hdr.type != HCI_SCO_DATA_PKT) {
588 1.1.4.2 chap printf("%s: bad SCO packet type\n", unit->hci_devname);
589 1.1.4.2 chap goto bad;
590 1.1.4.2 chap }
591 1.1.4.2 chap
592 1.1.4.2 chap if (m->m_pkthdr.len != hdr.length) {
593 1.1.4.2 chap printf("%s: bad SCO packet length (%d != %d)\n", unit->hci_devname, m->m_pkthdr.len, hdr.length);
594 1.1.4.2 chap goto bad;
595 1.1.4.2 chap }
596 1.1.4.2 chap #endif
597 1.1.4.2 chap
598 1.1.4.2 chap hdr.con_handle = le16toh(hdr.con_handle);
599 1.1.4.2 chap handle = HCI_CON_HANDLE(hdr.con_handle);
600 1.1.4.2 chap
601 1.1.4.2 chap link = hci_link_lookup_handle(unit, handle);
602 1.1.4.2 chap if (link == NULL || link->hl_type == HCI_LINK_ACL) {
603 1.1.4.2 chap DPRINTF("%s: dumping packet for unknown handle #%d\n",
604 1.1.4.2 chap unit->hci_devname, handle);
605 1.1.4.2 chap
606 1.1.4.2 chap goto bad;
607 1.1.4.2 chap }
608 1.1.4.2 chap
609 1.1.4.2 chap (*link->hl_sco->sp_proto->input)(link->hl_sco->sp_upper, m);
610 1.1.4.2 chap return;
611 1.1.4.2 chap
612 1.1.4.2 chap bad:
613 1.1.4.2 chap m_freem(m);
614 1.1.4.2 chap }
615 1.1.4.2 chap
616 1.1.4.2 chap void
617 1.1.4.2 chap hci_sco_start(struct hci_link *link)
618 1.1.4.2 chap {
619 1.1.4.2 chap }
620 1.1.4.2 chap
621 1.1.4.2 chap /*
622 1.1.4.2 chap * SCO packets have completed at the controller, so we can
623 1.1.4.2 chap * signal up to free the buffer space.
624 1.1.4.2 chap */
625 1.1.4.2 chap void
626 1.1.4.2 chap hci_sco_complete(struct hci_link *link, int num)
627 1.1.4.2 chap {
628 1.1.4.2 chap
629 1.1.4.2 chap DPRINTFN(5, "handle #%d (num=%d)\n", link->hl_handle, num);
630 1.1.4.2 chap link->hl_sco->sp_pending--;
631 1.1.4.2 chap (*link->hl_sco->sp_proto->complete)(link->hl_sco->sp_upper, num);
632 1.1.4.2 chap }
633 1.1.4.2 chap
634 1.1.4.2 chap /*******************************************************************************
635 1.1.4.2 chap *
636 1.1.4.2 chap * Generic HCI Connection alloc/free/lookup etc
637 1.1.4.2 chap */
638 1.1.4.2 chap
639 1.1.4.2 chap struct hci_link *
640 1.1.4.2 chap hci_link_alloc(struct hci_unit *unit)
641 1.1.4.2 chap {
642 1.1.4.2 chap struct hci_link *link;
643 1.1.4.2 chap
644 1.1.4.2 chap KASSERT(unit);
645 1.1.4.2 chap
646 1.1.4.2 chap link = malloc(sizeof(struct hci_link), M_BLUETOOTH, M_NOWAIT | M_ZERO);
647 1.1.4.2 chap if (link == NULL)
648 1.1.4.2 chap return NULL;
649 1.1.4.2 chap
650 1.1.4.2 chap link->hl_unit = unit;
651 1.1.4.2 chap link->hl_state = HCI_LINK_CLOSED;
652 1.1.4.2 chap
653 1.1.4.2 chap /* init ACL portion */
654 1.1.4.2 chap callout_init(&link->hl_expire);
655 1.1.4.2 chap callout_setfunc(&link->hl_expire, hci_acl_timeout, link);
656 1.1.4.2 chap
657 1.1.4.2 chap TAILQ_INIT(&link->hl_txq); /* outgoing packets */
658 1.1.4.2 chap TAILQ_INIT(&link->hl_reqs); /* request queue */
659 1.1.4.2 chap
660 1.1.4.2 chap link->hl_mtu = L2CAP_MTU_DEFAULT; /* L2CAP signal mtu */
661 1.1.4.2 chap link->hl_flush = L2CAP_FLUSH_TIMO_DEFAULT; /* flush timeout */
662 1.1.4.2 chap
663 1.1.4.2 chap /* init SCO portion */
664 1.1.4.2 chap MBUFQ_INIT(&link->hl_data);
665 1.1.4.2 chap
666 1.1.4.2 chap /* attach to unit */
667 1.1.4.2 chap TAILQ_INSERT_HEAD(&unit->hci_links, link, hl_next);
668 1.1.4.2 chap return link;
669 1.1.4.2 chap }
670 1.1.4.2 chap
671 1.1.4.2 chap void
672 1.1.4.2 chap hci_link_free(struct hci_link *link, int err)
673 1.1.4.2 chap {
674 1.1.4.2 chap struct l2cap_req *req;
675 1.1.4.2 chap struct l2cap_pdu *pdu;
676 1.1.4.2 chap struct l2cap_channel *chan;
677 1.1.4.2 chap
678 1.1.4.2 chap KASSERT(link);
679 1.1.4.2 chap
680 1.1.4.2 chap DPRINTF("#%d, type = %d, state = %d, refcnt = %d\n",
681 1.1.4.2 chap link->hl_handle, link->hl_type,
682 1.1.4.2 chap link->hl_state, link->hl_refcnt);
683 1.1.4.2 chap
684 1.1.4.2 chap /* ACL reference count */
685 1.1.4.2 chap if (link->hl_refcnt > 0) {
686 1.1.4.2 chap LIST_FOREACH(chan, &l2cap_active_list, lc_ncid) {
687 1.1.4.2 chap if (chan->lc_link == link)
688 1.1.4.2 chap l2cap_close(chan, err);
689 1.1.4.2 chap }
690 1.1.4.2 chap }
691 1.1.4.2 chap KASSERT(link->hl_refcnt == 0);
692 1.1.4.2 chap
693 1.1.4.2 chap /* ACL L2CAP requests.. */
694 1.1.4.2 chap while ((req = TAILQ_FIRST(&link->hl_reqs)) != NULL)
695 1.1.4.2 chap l2cap_request_free(req);
696 1.1.4.2 chap
697 1.1.4.2 chap KASSERT(TAILQ_EMPTY(&link->hl_reqs));
698 1.1.4.2 chap
699 1.1.4.2 chap /* ACL outgoing data queue */
700 1.1.4.2 chap while ((pdu = TAILQ_FIRST(&link->hl_txq)) != NULL) {
701 1.1.4.2 chap TAILQ_REMOVE(&link->hl_txq, pdu, lp_next);
702 1.1.4.2 chap MBUFQ_DRAIN(&pdu->lp_data);
703 1.1.4.2 chap if (pdu->lp_pending)
704 1.1.4.2 chap link->hl_unit->hci_num_acl_pkts += pdu->lp_pending;
705 1.1.4.2 chap
706 1.1.4.2 chap pool_put(&l2cap_pdu_pool, pdu);
707 1.1.4.2 chap }
708 1.1.4.2 chap
709 1.1.4.2 chap KASSERT(TAILQ_EMPTY(&link->hl_txq));
710 1.1.4.2 chap
711 1.1.4.2 chap /* ACL incoming data packet */
712 1.1.4.2 chap if (link->hl_rxp != NULL) {
713 1.1.4.2 chap m_freem(link->hl_rxp);
714 1.1.4.2 chap link->hl_rxp = NULL;
715 1.1.4.2 chap }
716 1.1.4.2 chap
717 1.1.4.2 chap /* SCO master ACL link */
718 1.1.4.2 chap if (link->hl_link != NULL) {
719 1.1.4.2 chap hci_acl_close(link->hl_link, err);
720 1.1.4.2 chap link->hl_link = NULL;
721 1.1.4.2 chap }
722 1.1.4.2 chap
723 1.1.4.2 chap /* SCO pcb */
724 1.1.4.2 chap if (link->hl_sco != NULL) {
725 1.1.4.2 chap struct sco_pcb *pcb;
726 1.1.4.2 chap
727 1.1.4.2 chap pcb = link->hl_sco;
728 1.1.4.2 chap pcb->sp_link = NULL;
729 1.1.4.2 chap link->hl_sco = NULL;
730 1.1.4.2 chap (*pcb->sp_proto->disconnected)(pcb->sp_upper, err);
731 1.1.4.2 chap }
732 1.1.4.2 chap
733 1.1.4.2 chap /* flush any SCO data */
734 1.1.4.2 chap MBUFQ_DRAIN(&link->hl_data);
735 1.1.4.2 chap
736 1.1.4.2 chap /*
737 1.1.4.2 chap * Halt the callout - if its already running we cannot free the
738 1.1.4.2 chap * link structure but the timeout function will call us back in
739 1.1.4.2 chap * any case.
740 1.1.4.2 chap */
741 1.1.4.2 chap link->hl_state = HCI_LINK_CLOSED;
742 1.1.4.2 chap callout_stop(&link->hl_expire);
743 1.1.4.2 chap if (callout_invoking(&link->hl_expire))
744 1.1.4.2 chap return;
745 1.1.4.2 chap
746 1.1.4.2 chap TAILQ_REMOVE(&link->hl_unit->hci_links, link, hl_next);
747 1.1.4.2 chap free(link, M_BLUETOOTH);
748 1.1.4.2 chap }
749 1.1.4.2 chap
750 1.1.4.2 chap /*
751 1.1.4.2 chap * Lookup HCI link by address and type. Note that for SCO links there may
752 1.1.4.2 chap * be more than one link per address, so we only return links with no
753 1.1.4.2 chap * handle (ie new links)
754 1.1.4.2 chap */
755 1.1.4.2 chap struct hci_link *
756 1.1.4.2 chap hci_link_lookup_bdaddr(struct hci_unit *unit, bdaddr_t *bdaddr, uint16_t type)
757 1.1.4.2 chap {
758 1.1.4.2 chap struct hci_link *link;
759 1.1.4.2 chap
760 1.1.4.2 chap KASSERT(unit);
761 1.1.4.2 chap KASSERT(bdaddr);
762 1.1.4.2 chap
763 1.1.4.2 chap TAILQ_FOREACH(link, &unit->hci_links, hl_next) {
764 1.1.4.2 chap if (link->hl_type != type)
765 1.1.4.2 chap continue;
766 1.1.4.2 chap
767 1.1.4.2 chap if (type == HCI_LINK_SCO && link->hl_handle != 0)
768 1.1.4.2 chap continue;
769 1.1.4.2 chap
770 1.1.4.2 chap if (bdaddr_same(&link->hl_bdaddr, bdaddr))
771 1.1.4.2 chap break;
772 1.1.4.2 chap }
773 1.1.4.2 chap
774 1.1.4.2 chap return link;
775 1.1.4.2 chap }
776 1.1.4.2 chap
777 1.1.4.2 chap struct hci_link *
778 1.1.4.2 chap hci_link_lookup_handle(struct hci_unit *unit, uint16_t handle)
779 1.1.4.2 chap {
780 1.1.4.2 chap struct hci_link *link;
781 1.1.4.2 chap
782 1.1.4.2 chap KASSERT(unit);
783 1.1.4.2 chap
784 1.1.4.2 chap TAILQ_FOREACH(link, &unit->hci_links, hl_next) {
785 1.1.4.2 chap if (handle == link->hl_handle)
786 1.1.4.2 chap break;
787 1.1.4.2 chap }
788 1.1.4.2 chap
789 1.1.4.2 chap return link;
790 1.1.4.2 chap }
791