1 /* $NetBSD: rfcomm_dlc.c,v 1.1 2006/06/19 15:44:45 gdamore Exp $ */ 2 3 /*- 4 * Copyright (c) 2006 Itronix Inc. 5 * All rights reserved. 6 * 7 * Written by Iain Hibbert for Itronix Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of Itronix Inc. may not be used to endorse 18 * or promote products derived from this software without specific 19 * prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY 25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 28 * ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: rfcomm_dlc.c,v 1.1 2006/06/19 15:44:45 gdamore Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/kernel.h> 39 #include <sys/mbuf.h> 40 #include <sys/proc.h> 41 #include <sys/systm.h> 42 43 #include <netbt/bluetooth.h> 44 #include <netbt/hci.h> 45 #include <netbt/l2cap.h> 46 #include <netbt/rfcomm.h> 47 48 /* 49 * rfcomm_dlc_lookup(rfcomm_session, dlci) 50 * 51 * Find DLC on session with matching dlci 52 */ 53 struct rfcomm_dlc * 54 rfcomm_dlc_lookup(struct rfcomm_session *rs, int dlci) 55 { 56 struct rfcomm_dlc *dlc; 57 58 LIST_FOREACH(dlc, &rs->rs_dlcs, rd_next) { 59 if (dlc->rd_dlci == dlci) 60 break; 61 } 62 63 return dlc; 64 } 65 66 /* 67 * rfcomm_dlc_newconn(rfcomm_session, dlci) 68 * 69 * handle a new dlc request (since its called from a couple of places) 70 */ 71 struct rfcomm_dlc * 72 rfcomm_dlc_newconn(struct rfcomm_session *rs, int dlci) 73 { 74 struct rfcomm_session *ls; 75 struct rfcomm_dlc *dlc, *any, *best; 76 struct sockaddr_bt laddr, raddr, addr; 77 int chan; 78 79 /* 80 * Search amongst the listening DLC community for the best match for 81 * address & channel. We keep listening DLC's hanging on listening 82 * sessions in a last first order, so scan the entire bunch and keep 83 * a note of the best address and BDADDR_ANY matches in order to find 84 * the oldest and most specific match. 85 */ 86 l2cap_sockaddr(rs->rs_l2cap, &laddr); 87 l2cap_peeraddr(rs->rs_l2cap, &raddr); 88 chan = RFCOMM_CHANNEL(dlci); 89 90 any = best = NULL; 91 LIST_FOREACH(ls, &rfcomm_session_listen, rs_next) { 92 l2cap_sockaddr(ls->rs_l2cap, &addr); 93 94 if (addr.bt_psm != laddr.bt_psm) 95 continue; 96 97 if (bdaddr_same(&laddr.bt_bdaddr, &addr.bt_bdaddr)) { 98 LIST_FOREACH(dlc, &ls->rs_dlcs, rd_next) { 99 if (dlc->rd_laddr.bt_channel == chan) 100 best = dlc; 101 } 102 } 103 104 if (bdaddr_any(&addr.bt_bdaddr)) { 105 LIST_FOREACH(dlc, &ls->rs_dlcs, rd_next) { 106 if (dlc->rd_laddr.bt_channel == chan) 107 any = dlc; 108 } 109 } 110 } 111 112 dlc = best ? best : any; 113 114 // XXX 115 // Note that if this fails, we could have missed a chance to open 116 // a connection - really need to rewrite the strategy for storing 117 // listening DLC's so all can be checked in turn.. 118 // 119 if (dlc != NULL) 120 dlc = (*dlc->rd_proto->newconn)(dlc->rd_upper, &laddr, &raddr); 121 122 if (dlc == NULL) { 123 rfcomm_session_send_frame(rs, RFCOMM_FRAME_DM, dlci); 124 return NULL; 125 } 126 127 dlc->rd_dlci = dlci; 128 dlc->rd_mtu = rfcomm_mtu_default; 129 130 memcpy(&dlc->rd_laddr, &laddr, sizeof(struct sockaddr_bt)); 131 dlc->rd_laddr.bt_channel = chan; 132 133 memcpy(&dlc->rd_raddr, &raddr, sizeof(struct sockaddr_bt)); 134 dlc->rd_raddr.bt_channel = chan; 135 136 dlc->rd_session = rs; 137 dlc->rd_state = RFCOMM_DLC_WAIT_CONNECT; 138 LIST_INSERT_HEAD(&rs->rs_dlcs, dlc, rd_next); 139 140 return dlc; 141 } 142 143 /* 144 * rfcomm_dlc_close(dlc, error) 145 * 146 * detach DLC from session and clean up 147 */ 148 void 149 rfcomm_dlc_close(struct rfcomm_dlc *dlc, int err) 150 { 151 struct rfcomm_session *rs; 152 struct rfcomm_credit *credit; 153 154 KASSERT(dlc->rd_state != RFCOMM_DLC_CLOSED); 155 156 /* Clear credit history */ 157 rs = dlc->rd_session; 158 SIMPLEQ_FOREACH(credit, &rs->rs_credits, rc_next) 159 if (credit->rc_dlc == dlc) 160 credit->rc_dlc = NULL; 161 162 callout_stop(&dlc->rd_timeout); 163 164 LIST_REMOVE(dlc, rd_next); 165 dlc->rd_session = NULL; 166 dlc->rd_state = RFCOMM_DLC_CLOSED; 167 168 (*dlc->rd_proto->disconnected)(dlc->rd_upper, err); 169 170 /* 171 * It is the responsibility of the party who sends the last 172 * DISC(dlci) to disconnect the session, but we will schedule 173 * an expiry just in case that doesnt happen.. 174 */ 175 if (LIST_EMPTY(&rs->rs_dlcs)) { 176 if (rs->rs_state == RFCOMM_SESSION_LISTEN) 177 rfcomm_session_free(rs); 178 else 179 callout_schedule(&rs->rs_timeout, 180 rfcomm_ack_timeout * hz); 181 } 182 } 183 184 /* 185 * rfcomm_dlc_timeout(dlc) 186 * 187 * DLC timeout function is schedUled when we sent any of SABM, 188 * DISC, MCC_MSC, or MCC_PN and should be cancelled when we get 189 * the relevant response. There is nothing to do but shut this 190 * DLC down. 191 */ 192 void 193 rfcomm_dlc_timeout(void *arg) 194 { 195 struct rfcomm_dlc *dlc = arg; 196 int s; 197 198 s = splsoftnet(); 199 callout_ack(&dlc->rd_timeout); 200 201 if (dlc->rd_state != RFCOMM_DLC_CLOSED) 202 rfcomm_dlc_close(dlc, ETIMEDOUT); 203 else if (dlc->rd_flags & RFCOMM_DLC_DETACH) 204 free(dlc, M_BLUETOOTH); 205 206 splx(s); 207 } 208 209 /* 210 * rfcomm_dlc_connect(rfcomm_dlc) 211 * 212 * initiate DLC connection (session is already connected) 213 */ 214 int 215 rfcomm_dlc_connect(struct rfcomm_dlc *dlc) 216 { 217 struct rfcomm_mcc_pn pn; 218 int err = 0; 219 220 KASSERT(dlc->rd_session != NULL); 221 KASSERT(dlc->rd_session->rs_state == RFCOMM_SESSION_OPEN); 222 KASSERT(dlc->rd_state == RFCOMM_DLC_WAIT_SESSION); 223 224 /* 225 * If we have not already sent a PN on the session, we must send 226 * a PN to negotiate Credit Flow Control, and this setting will 227 * apply to all future connections for this session. We ask for 228 * this every time. 229 */ 230 memset(&pn, 0, sizeof(pn)); 231 pn.dlci = dlc->rd_dlci; 232 pn.priority = dlc->rd_dlci | 0x07; 233 pn.mtu = htole16(dlc->rd_mtu); 234 235 pn.flow_control = 0xf0; 236 dlc->rd_rxcred = (dlc->rd_rxsize / dlc->rd_mtu); 237 dlc->rd_rxcred = min(dlc->rd_rxcred, RFCOMM_CREDITS_DEFAULT); 238 pn.credits = dlc->rd_rxcred; 239 240 err = rfcomm_session_send_mcc(dlc->rd_session, 1, 241 RFCOMM_MCC_PN, &pn, sizeof(pn)); 242 if (err) 243 return err; 244 245 dlc->rd_state = RFCOMM_DLC_WAIT_CONNECT; 246 callout_schedule(&dlc->rd_timeout, rfcomm_mcc_timeout * hz); 247 248 return 0; 249 } 250 251 /* 252 * rfcomm_dlc_start(rfcomm_dlc) 253 * 254 * Start sending data (and/or credits) for DLC. Our strategy is to 255 * send anything we can down to the l2cap layer. When credits run 256 * out, data will naturally bunch up. When not using credit flow 257 * control, we limit the number of packets we have pending to reduce 258 * flow control lag. 259 * We should deal with channel priority somehow. 260 */ 261 void 262 rfcomm_dlc_start(struct rfcomm_dlc *dlc) 263 { 264 struct rfcomm_session *rs = dlc->rd_session; 265 struct mbuf *m; 266 int len, credits; 267 268 KASSERT(rs != NULL); 269 KASSERT(rs->rs_state == RFCOMM_SESSION_OPEN); 270 KASSERT(dlc->rd_state == RFCOMM_DLC_OPEN); 271 272 for (;;) { 273 credits = 0; 274 len = dlc->rd_mtu; 275 if (rs->rs_flags & RFCOMM_SESSION_CFC) { 276 credits = (dlc->rd_rxsize / dlc->rd_mtu); 277 credits -= dlc->rd_rxcred; 278 credits = min(credits, RFCOMM_CREDITS_MAX); 279 280 if (credits > 0) 281 len--; 282 283 if (dlc->rd_txcred == 0) 284 len = 0; 285 } else { 286 if (rs->rs_flags & RFCOMM_SESSION_RFC) 287 break; 288 289 if (dlc->rd_rmodem & RFCOMM_MSC_FC) 290 break; 291 292 if (dlc->rd_pending > RFCOMM_CREDITS_DEFAULT) 293 break; 294 } 295 296 if (dlc->rd_txbuf == NULL) 297 len = 0; 298 299 if (len == 0) { 300 if (credits == 0) 301 break; 302 303 /* 304 * No need to send small numbers of credits on their 305 * own unless the other end hasn't many left. 306 */ 307 if (credits < RFCOMM_CREDITS_DEFAULT 308 && dlc->rd_rxcred > RFCOMM_CREDITS_DEFAULT) 309 break; 310 311 m = NULL; 312 } else { 313 /* 314 * take what data we can from (front of) txbuf 315 */ 316 m = dlc->rd_txbuf; 317 if (len < m->m_pkthdr.len) { 318 dlc->rd_txbuf = m_split(m, len, M_DONTWAIT); 319 if (dlc->rd_txbuf == NULL) { 320 dlc->rd_txbuf = m; 321 break; 322 } 323 } else { 324 dlc->rd_txbuf = NULL; 325 len = m->m_pkthdr.len; 326 } 327 } 328 329 DPRINTFN(10, "dlci %d send %d bytes, %d credits, rxcred = %d\n", 330 dlc->rd_dlci, len, credits, dlc->rd_rxcred); 331 332 if (rfcomm_session_send_uih(rs, dlc, credits, m)) { 333 printf("%s: lost %d bytes on DLCI %d\n", 334 __func__, len, dlc->rd_dlci); 335 336 break; 337 } 338 339 dlc->rd_pending++; 340 341 if (rs->rs_flags & RFCOMM_SESSION_CFC) { 342 if (len > 0) 343 dlc->rd_txcred--; 344 345 if (credits > 0) 346 dlc->rd_rxcred += credits; 347 } 348 } 349 } 350