1 /* $NetBSD: sco_upper.c,v 1.7 2008/03/16 23:28:10 plunky 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: sco_upper.c,v 1.7 2008/03/16 23:28:10 plunky 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/sco.h> 46 47 /**************************************************************************** 48 * 49 * SCO - Upper Protocol API 50 */ 51 52 struct sco_pcb_list sco_pcb = LIST_HEAD_INITIALIZER(sco_pcb); 53 54 /* 55 * sco_attach(handle, proto, upper) 56 * 57 * Attach a new instance of SCO pcb to handle 58 */ 59 int 60 sco_attach(struct sco_pcb **handle, 61 const struct btproto *proto, void *upper) 62 { 63 struct sco_pcb *pcb; 64 65 KASSERT(handle != NULL); 66 KASSERT(proto != NULL); 67 KASSERT(upper != NULL); 68 69 pcb = malloc(sizeof(struct sco_pcb), M_BLUETOOTH, 70 M_NOWAIT | M_ZERO); 71 if (pcb == NULL) 72 return ENOMEM; 73 74 pcb->sp_proto = proto; 75 pcb->sp_upper = upper; 76 77 LIST_INSERT_HEAD(&sco_pcb, pcb, sp_next); 78 79 *handle = pcb; 80 return 0; 81 } 82 83 /* 84 * sco_bind(pcb, sockaddr) 85 * 86 * Bind SCO pcb to local address 87 */ 88 int 89 sco_bind(struct sco_pcb *pcb, struct sockaddr_bt *addr) 90 { 91 92 bdaddr_copy(&pcb->sp_laddr, &addr->bt_bdaddr); 93 return 0; 94 } 95 96 /* 97 * sco_sockaddr(pcb, sockaddr) 98 * 99 * Copy local address of PCB to sockaddr 100 */ 101 int 102 sco_sockaddr(struct sco_pcb *pcb, struct sockaddr_bt *addr) 103 { 104 105 memset(addr, 0, sizeof(struct sockaddr_bt)); 106 addr->bt_len = sizeof(struct sockaddr_bt); 107 addr->bt_family = AF_BLUETOOTH; 108 bdaddr_copy(&addr->bt_bdaddr, &pcb->sp_laddr); 109 return 0; 110 } 111 112 /* 113 * sco_connect(pcb, sockaddr) 114 * 115 * Initiate a SCO connection to the destination address. 116 */ 117 int 118 sco_connect(struct sco_pcb *pcb, struct sockaddr_bt *dest) 119 { 120 hci_add_sco_con_cp cp; 121 struct hci_unit *unit; 122 struct hci_link *acl, *sco; 123 int err; 124 125 if (pcb->sp_flags & SP_LISTENING) 126 return EINVAL; 127 128 bdaddr_copy(&pcb->sp_raddr, &dest->bt_bdaddr); 129 130 if (bdaddr_any(&pcb->sp_raddr)) 131 return EDESTADDRREQ; 132 133 if (bdaddr_any(&pcb->sp_laddr)) { 134 err = hci_route_lookup(&pcb->sp_laddr, &pcb->sp_raddr); 135 if (err) 136 return err; 137 } 138 139 unit = hci_unit_lookup(&pcb->sp_laddr); 140 if (unit == NULL) 141 return ENETDOWN; 142 143 /* 144 * We must have an already open ACL connection before we open the SCO 145 * connection, and since SCO connections dont happen on their own we 146 * will not open one, the application wanting this should have opened 147 * it previously. 148 */ 149 acl = hci_link_lookup_bdaddr(unit, &pcb->sp_raddr, HCI_LINK_ACL); 150 if (acl == NULL || acl->hl_state != HCI_LINK_OPEN) 151 return EHOSTUNREACH; 152 153 sco = hci_link_alloc(unit, &pcb->sp_raddr, HCI_LINK_SCO); 154 if (sco == NULL) 155 return ENOMEM; 156 157 sco->hl_link = hci_acl_open(unit, &pcb->sp_raddr); 158 KASSERT(sco->hl_link == acl); 159 160 cp.con_handle = htole16(acl->hl_handle); 161 cp.pkt_type = htole16(0x00e0); /* HV1, HV2, HV3 */ 162 err = hci_send_cmd(unit, HCI_CMD_ADD_SCO_CON, &cp, sizeof(cp)); 163 if (err) { 164 hci_link_free(sco, err); 165 return err; 166 } 167 168 sco->hl_sco = pcb; 169 pcb->sp_link = sco; 170 171 pcb->sp_mtu = unit->hci_max_sco_size; 172 return 0; 173 } 174 175 /* 176 * sco_peeraddr(pcb, sockaddr) 177 * 178 * Copy remote address of SCO pcb to sockaddr 179 */ 180 int 181 sco_peeraddr(struct sco_pcb *pcb, struct sockaddr_bt *addr) 182 { 183 184 memset(addr, 0, sizeof(struct sockaddr_bt)); 185 addr->bt_len = sizeof(struct sockaddr_bt); 186 addr->bt_family = AF_BLUETOOTH; 187 bdaddr_copy(&addr->bt_bdaddr, &pcb->sp_raddr); 188 return 0; 189 } 190 191 /* 192 * sco_disconnect(pcb, linger) 193 * 194 * Initiate disconnection of connected SCO pcb 195 */ 196 int 197 sco_disconnect(struct sco_pcb *pcb, int linger) 198 { 199 hci_discon_cp cp; 200 struct hci_link *sco; 201 int err; 202 203 sco = pcb->sp_link; 204 if (sco == NULL) 205 return EINVAL; 206 207 cp.con_handle = htole16(sco->hl_handle); 208 cp.reason = 0x13; /* "Remote User Terminated Connection" */ 209 210 err = hci_send_cmd(sco->hl_unit, HCI_CMD_DISCONNECT, &cp, sizeof(cp)); 211 if (err || linger == 0) { 212 sco->hl_sco = NULL; 213 pcb->sp_link = NULL; 214 hci_link_free(sco, err); 215 } 216 217 return err; 218 } 219 220 /* 221 * sco_detach(handle) 222 * 223 * Detach SCO pcb from handle and clear up 224 */ 225 int 226 sco_detach(struct sco_pcb **handle) 227 { 228 struct sco_pcb *pcb; 229 230 KASSERT(handle != NULL); 231 pcb = *handle; 232 *handle = NULL; 233 234 if (pcb == NULL) 235 return EINVAL; 236 237 if (pcb->sp_link != NULL) { 238 sco_disconnect(pcb, 0); 239 pcb->sp_link = NULL; 240 } 241 242 LIST_REMOVE(pcb, sp_next); 243 free(pcb, M_BLUETOOTH); 244 return 0; 245 } 246 247 /* 248 * sco_listen(pcb) 249 * 250 * Mark pcb as a listener. 251 */ 252 int 253 sco_listen(struct sco_pcb *pcb) 254 { 255 256 if (pcb->sp_link != NULL) 257 return EINVAL; 258 259 pcb->sp_flags |= SP_LISTENING; 260 return 0; 261 } 262 263 /* 264 * sco_send(pcb, mbuf) 265 * 266 * Send data on SCO pcb. 267 * 268 * Gross hackage, we just output the packet directly onto the unit queue. 269 * This will work fine for one channel per unit, but for more channels it 270 * really needs fixing. We set the context so that when the packet is sent, 271 * we can drop a record from the socket buffer. 272 */ 273 int 274 sco_send(struct sco_pcb *pcb, struct mbuf *m) 275 { 276 hci_scodata_hdr_t *hdr; 277 int plen; 278 279 if (pcb->sp_link == NULL) { 280 m_freem(m); 281 return EINVAL; 282 } 283 284 plen = m->m_pkthdr.len; 285 DPRINTFN(10, "%d bytes\n", plen); 286 287 /* 288 * This is a temporary limitation, as USB devices cannot 289 * handle SCO packet sizes that are not an integer number 290 * of Isochronous frames. See ubt(4) 291 */ 292 if (plen != pcb->sp_mtu) { 293 m_freem(m); 294 return EMSGSIZE; 295 } 296 297 M_PREPEND(m, sizeof(hci_scodata_hdr_t), M_DONTWAIT); 298 if (m == NULL) 299 return ENOMEM; 300 301 hdr = mtod(m, hci_scodata_hdr_t *); 302 hdr->type = HCI_SCO_DATA_PKT; 303 hdr->con_handle = htole16(pcb->sp_link->hl_handle); 304 hdr->length = plen; 305 306 pcb->sp_pending++; 307 M_SETCTX(m, pcb->sp_link); 308 hci_output_sco(pcb->sp_link->hl_unit, m); 309 310 return 0; 311 } 312 313 /* 314 * sco_setopt(pcb, option, addr) 315 * 316 * Set SCO pcb options 317 */ 318 int 319 sco_setopt(struct sco_pcb *pcb, int opt, void *addr) 320 { 321 int err = 0; 322 323 switch (opt) { 324 default: 325 err = ENOPROTOOPT; 326 break; 327 } 328 329 return err; 330 } 331 332 /* 333 * sco_getopt(pcb, option, addr) 334 * 335 * Get SCO pcb options 336 */ 337 int 338 sco_getopt(struct sco_pcb *pcb, int opt, void *addr) 339 { 340 341 switch (opt) { 342 case SO_SCO_MTU: 343 *(uint16_t *)addr = pcb->sp_mtu; 344 return sizeof(uint16_t); 345 346 case SO_SCO_HANDLE: 347 if (pcb->sp_link) { 348 *(uint16_t *)addr = pcb->sp_link->hl_handle; 349 return sizeof(uint16_t); 350 } 351 break; 352 353 default: 354 break; 355 } 356 return 0; 357 } 358