1 /* $OpenBSD: rfcomm_dlc.c,v 1.1 2007/06/01 02:46:12 uwe Exp $ */
2 /* $NetBSD: rfcomm_dlc.c,v 1.3 2007/04/21 06:15:23 plunky Exp $ */
3 /* $DragonFly: src/sys/netbt/rfcomm_dlc.c,v 1.1 2007/12/30 20:02:56 hasso Exp $ */
6 * Copyright (c) 2006 Itronix Inc.
9 * Written by Iain Hibbert for Itronix Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of Itronix Inc. may not be used to endorse
20 * or promote products derived from this software without specific
21 * prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
27 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include <sys/kernel.h>
42 #include <sys/systm.h>
43 #include <sys/endian.h>
45 #include <netbt/bluetooth.h>
46 #include <netbt/hci.h>
47 #include <netbt/l2cap.h>
48 #include <netbt/rfcomm.h>
51 * rfcomm_dlc_lookup(rfcomm_session, dlci)
53 * Find DLC on session with matching dlci
56 rfcomm_dlc_lookup(struct rfcomm_session *rs, int dlci)
58 struct rfcomm_dlc *dlc;
60 LIST_FOREACH(dlc, &rs->rs_dlcs, rd_next) {
61 if (dlc->rd_dlci == dlci)
69 * rfcomm_dlc_newconn(rfcomm_session, dlci)
71 * handle a new dlc request (since its called from a couple of places)
74 rfcomm_dlc_newconn(struct rfcomm_session *rs, int dlci)
76 struct rfcomm_session *ls;
77 struct rfcomm_dlc *new, *dlc, *any, *best;
78 struct sockaddr_bt laddr, raddr, addr;
82 * Search amongst the listening DLC community for the best match for
83 * address & channel. We keep listening DLC's hanging on listening
84 * sessions in a last first order, so scan the entire bunch and keep
85 * a note of the best address and BDADDR_ANY matches in order to find
86 * the oldest and most specific match.
88 l2cap_sockaddr(rs->rs_l2cap, &laddr);
89 l2cap_peeraddr(rs->rs_l2cap, &raddr);
90 chan = RFCOMM_CHANNEL(dlci);
94 LIST_FOREACH(ls, &rfcomm_session_listen, rs_next) {
95 l2cap_sockaddr(ls->rs_l2cap, &addr);
97 if (addr.bt_psm != laddr.bt_psm)
100 if (bdaddr_same(&laddr.bt_bdaddr, &addr.bt_bdaddr)) {
101 LIST_FOREACH(dlc, &ls->rs_dlcs, rd_next) {
102 if (dlc->rd_laddr.bt_channel == chan)
107 if (bdaddr_any(&addr.bt_bdaddr)) {
108 LIST_FOREACH(dlc, &ls->rs_dlcs, rd_next) {
109 if (dlc->rd_laddr.bt_channel == chan)
115 dlc = best ? best : any;
118 * Note that if this fails, we could have missed a chance to open
119 * a connection - really need to rewrite the strategy for storing
120 * listening DLC's so all can be checked in turn..
123 new = (*dlc->rd_proto->newconn)(dlc->rd_upper, &laddr, &raddr);
126 rfcomm_session_send_frame(rs, RFCOMM_FRAME_DM, dlci);
131 new->rd_mtu = rfcomm_mtu_default;
132 new->rd_mode = dlc->rd_mode;
134 memcpy(&new->rd_laddr, &laddr, sizeof(struct sockaddr_bt));
135 new->rd_laddr.bt_channel = chan;
137 memcpy(&new->rd_raddr, &raddr, sizeof(struct sockaddr_bt));
138 new->rd_raddr.bt_channel = chan;
140 new->rd_session = rs;
141 new->rd_state = RFCOMM_DLC_WAIT_CONNECT;
142 LIST_INSERT_HEAD(&rs->rs_dlcs, new, rd_next);
148 * rfcomm_dlc_close(dlc, error)
150 * detach DLC from session and clean up
153 rfcomm_dlc_close(struct rfcomm_dlc *dlc, int err)
155 struct rfcomm_session *rs;
156 struct rfcomm_credit *credit;
158 KKASSERT(dlc->rd_state != RFCOMM_DLC_CLOSED);
160 /* Clear credit history */
161 rs = dlc->rd_session;
162 STAILQ_FOREACH(credit, &rs->rs_credits, rc_next)
163 if (credit->rc_dlc == dlc)
164 credit->rc_dlc = NULL;
166 callout_stop(&dlc->rd_timeout);
168 LIST_REMOVE(dlc, rd_next);
169 dlc->rd_session = NULL;
170 dlc->rd_state = RFCOMM_DLC_CLOSED;
172 (*dlc->rd_proto->disconnected)(dlc->rd_upper, err);
175 * It is the responsibility of the party who sends the last
176 * DISC(dlci) to disconnect the session, but we will schedule
177 * an expiry just in case that doesnt happen..
179 if (LIST_EMPTY(&rs->rs_dlcs)) {
180 if (rs->rs_state == RFCOMM_SESSION_LISTEN)
181 rfcomm_session_free(rs);
183 callout_reset(&rs->rs_timeout, rfcomm_ack_timeout * hz,
184 rfcomm_session_timeout, rs);
189 * rfcomm_dlc_timeout(dlc)
191 * DLC timeout function is schedUled when we sent any of SABM,
192 * DISC, MCC_MSC, or MCC_PN and should be cancelled when we get
193 * the relevant response. There is nothing to do but shut this
197 rfcomm_dlc_timeout(void *arg)
199 struct rfcomm_dlc *dlc = arg;
203 if (dlc->rd_state != RFCOMM_DLC_CLOSED)
204 rfcomm_dlc_close(dlc, ETIMEDOUT);
205 else if (dlc->rd_flags & RFCOMM_DLC_DETACH)
206 kfree(dlc, M_BLUETOOTH);
212 * rfcomm_dlc_setmode(rfcomm_dlc)
214 * Set link mode for DLC. This is only called when the session is
215 * already open, so we don't need to worry about any previous mode
219 rfcomm_dlc_setmode(struct rfcomm_dlc *dlc)
223 KKASSERT(dlc->rd_session != NULL);
224 KKASSERT(dlc->rd_session->rs_state == RFCOMM_SESSION_OPEN);
226 DPRINTF("dlci %d, auth %s, encrypt %s, secure %s\n", dlc->rd_dlci,
227 (dlc->rd_mode & RFCOMM_LM_AUTH ? "yes" : "no"),
228 (dlc->rd_mode & RFCOMM_LM_ENCRYPT ? "yes" : "no"),
229 (dlc->rd_mode & RFCOMM_LM_SECURE ? "yes" : "no"));
231 if (dlc->rd_mode & RFCOMM_LM_AUTH)
232 mode |= L2CAP_LM_AUTH;
234 if (dlc->rd_mode & RFCOMM_LM_ENCRYPT)
235 mode |= L2CAP_LM_ENCRYPT;
237 if (dlc->rd_mode & RFCOMM_LM_SECURE)
238 mode |= L2CAP_LM_SECURE;
240 return l2cap_setopt(dlc->rd_session->rs_l2cap, SO_L2CAP_LM, &mode);
244 * rfcomm_dlc_connect(rfcomm_dlc)
246 * initiate DLC connection (session is already connected)
249 rfcomm_dlc_connect(struct rfcomm_dlc *dlc)
251 struct rfcomm_mcc_pn pn;
254 KKASSERT(dlc->rd_session != NULL);
255 KKASSERT(dlc->rd_session->rs_state == RFCOMM_SESSION_OPEN);
256 KKASSERT(dlc->rd_state == RFCOMM_DLC_WAIT_SESSION);
259 * If we have not already sent a PN on the session, we must send
260 * a PN to negotiate Credit Flow Control, and this setting will
261 * apply to all future connections for this session. We ask for
262 * this every time, in order to establish initial credits.
264 memset(&pn, 0, sizeof(pn));
265 pn.dlci = dlc->rd_dlci;
266 pn.priority = dlc->rd_dlci | 0x07;
267 pn.mtu = htole16(dlc->rd_mtu);
269 pn.flow_control = 0xf0;
270 dlc->rd_rxcred = (dlc->rd_rxsize / dlc->rd_mtu);
271 dlc->rd_rxcred = min(dlc->rd_rxcred, RFCOMM_CREDITS_DEFAULT);
272 pn.credits = dlc->rd_rxcred;
274 err = rfcomm_session_send_mcc(dlc->rd_session, 1,
275 RFCOMM_MCC_PN, &pn, sizeof(pn));
279 dlc->rd_state = RFCOMM_DLC_WAIT_CONNECT;
280 callout_reset(&dlc->rd_timeout, rfcomm_mcc_timeout * hz,
281 rfcomm_dlc_timeout, dlc);
286 * rfcomm_dlc_open(rfcomm_dlc)
288 * send "Modem Status Command" and mark DLC as open.
291 rfcomm_dlc_open(struct rfcomm_dlc *dlc)
293 struct rfcomm_mcc_msc msc;
296 KKASSERT(dlc->rd_session != NULL);
297 KKASSERT(dlc->rd_session->rs_state == RFCOMM_SESSION_OPEN);
299 memset(&msc, 0, sizeof(msc));
300 msc.address = RFCOMM_MKADDRESS(1, dlc->rd_dlci);
301 msc.modem = dlc->rd_lmodem & 0xfe; /* EA = 0 */
302 msc.brk = 0x00 | 0x01; /* EA = 1 */
304 err = rfcomm_session_send_mcc(dlc->rd_session, 1,
305 RFCOMM_MCC_MSC, &msc, sizeof(msc));
309 callout_reset(&dlc->rd_timeout, rfcomm_mcc_timeout * hz,
310 rfcomm_dlc_timeout, dlc);
312 dlc->rd_state = RFCOMM_DLC_OPEN;
313 (*dlc->rd_proto->connected)(dlc->rd_upper);
319 * rfcomm_dlc_start(rfcomm_dlc)
321 * Start sending data (and/or credits) for DLC. Our strategy is to
322 * send anything we can down to the l2cap layer. When credits run
323 * out, data will naturally bunch up. When not using credit flow
324 * control, we limit the number of packets we have pending to reduce
326 * We should deal with channel priority somehow.
329 rfcomm_dlc_start(struct rfcomm_dlc *dlc)
331 struct rfcomm_session *rs = dlc->rd_session;
335 KKASSERT(rs != NULL);
336 KKASSERT(rs->rs_state == RFCOMM_SESSION_OPEN);
337 KKASSERT(dlc->rd_state == RFCOMM_DLC_OPEN);
342 if (rs->rs_flags & RFCOMM_SESSION_CFC) {
343 credits = (dlc->rd_rxsize / dlc->rd_mtu);
344 credits -= dlc->rd_rxcred;
345 credits = min(credits, RFCOMM_CREDITS_MAX);
350 if (dlc->rd_txcred == 0)
353 if (rs->rs_flags & RFCOMM_SESSION_RFC)
356 if (dlc->rd_rmodem & RFCOMM_MSC_FC)
359 if (dlc->rd_pending > RFCOMM_CREDITS_DEFAULT)
363 if (dlc->rd_txbuf == NULL)
371 * No need to send small numbers of credits on their
372 * own unless the other end hasn't many left.
374 if (credits < RFCOMM_CREDITS_DEFAULT
375 && dlc->rd_rxcred > RFCOMM_CREDITS_DEFAULT)
381 * take what data we can from (front of) txbuf
384 if (len < m->m_pkthdr.len) {
385 dlc->rd_txbuf = m_split(m, len, MB_DONTWAIT);
386 if (dlc->rd_txbuf == NULL) {
391 dlc->rd_txbuf = NULL;
392 len = m->m_pkthdr.len;
396 DPRINTFN(10, "dlci %d send %d bytes, %d credits, rxcred = %d\n",
397 dlc->rd_dlci, len, credits, dlc->rd_rxcred);
399 if (rfcomm_session_send_uih(rs, dlc, credits, m)) {
400 kprintf("%s: lost %d bytes on DLCI %d\n",
401 __func__, len, dlc->rd_dlci);
408 if (rs->rs_flags & RFCOMM_SESSION_CFC) {
413 dlc->rd_rxcred += credits;