1 /* $OpenBSD: sco_upper.c,v 1.2 2007/10/01 16:39:30 krw Exp $ */
2 /* $NetBSD: sco_upper.c,v 1.6 2007/03/30 20:47:03 plunky Exp $ */
5 * Copyright (c) 2006 Itronix Inc.
8 * Written by Iain Hibbert for Itronix Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of Itronix Inc. may not be used to endorse
19 * or promote products derived from this software without specific
20 * prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/param.h>
36 #include <sys/kernel.h>
39 #include <sys/systm.h>
40 #include <sys/endian.h>
43 #include <netbt/bluetooth.h>
44 #include <netbt/hci.h>
45 #include <netbt/sco.h>
47 /****************************************************************************
49 * SCO - Upper Protocol API
52 struct sco_pcb_list sco_pcb = LIST_HEAD_INITIALIZER(sco_pcb);
55 * sco_attach(handle, proto, upper)
57 * Attach a new instance of SCO pcb to handle
60 sco_attach(struct sco_pcb **handle,
61 const struct btproto *proto, void *upper)
65 KKASSERT(handle != NULL);
66 KKASSERT(proto != NULL);
67 KKASSERT(upper != NULL);
69 pcb = kmalloc(sizeof(*pcb), M_BLUETOOTH, M_NOWAIT | M_ZERO);
73 pcb->sp_proto = proto;
74 pcb->sp_upper = upper;
76 LIST_INSERT_HEAD(&sco_pcb, pcb, sp_next);
83 * sco_bind(pcb, sockaddr)
85 * Bind SCO pcb to local address
88 sco_bind(struct sco_pcb *pcb, struct sockaddr_bt *addr)
91 bdaddr_copy(&pcb->sp_laddr, &addr->bt_bdaddr);
96 * sco_sockaddr(pcb, sockaddr)
98 * Copy local address of PCB to sockaddr
101 sco_sockaddr(struct sco_pcb *pcb, struct sockaddr_bt *addr)
104 memset(addr, 0, sizeof(struct sockaddr_bt));
105 addr->bt_len = sizeof(struct sockaddr_bt);
106 addr->bt_family = AF_BLUETOOTH;
107 bdaddr_copy(&addr->bt_bdaddr, &pcb->sp_laddr);
112 * sco_connect(pcb, sockaddr)
114 * Initiate a SCO connection to the destination address.
117 sco_connect(struct sco_pcb *pcb, struct sockaddr_bt *dest)
119 hci_add_sco_con_cp cp;
120 struct hci_unit *unit;
121 struct hci_link *acl, *sco;
124 if (pcb->sp_flags & SP_LISTENING)
127 bdaddr_copy(&pcb->sp_raddr, &dest->bt_bdaddr);
129 if (bdaddr_any(&pcb->sp_raddr))
132 if (bdaddr_any(&pcb->sp_laddr)) {
133 err = hci_route_lookup(&pcb->sp_laddr, &pcb->sp_raddr);
138 unit = hci_unit_lookup(&pcb->sp_laddr);
143 * We must have an already open ACL connection before we open the SCO
144 * connection, and since SCO connections dont happen on their own we
145 * will not open one, the application wanting this should have opened
148 acl = hci_link_lookup_bdaddr(unit, &pcb->sp_raddr, HCI_LINK_ACL);
149 if (acl == NULL || acl->hl_state != HCI_LINK_OPEN)
152 sco = hci_link_alloc(unit);
156 sco->hl_type = HCI_LINK_SCO;
157 bdaddr_copy(&sco->hl_bdaddr, &pcb->sp_raddr);
159 sco->hl_link = hci_acl_open(unit, &pcb->sp_raddr);
160 KKASSERT(sco->hl_link == acl);
162 cp.con_handle = htole16(acl->hl_handle);
163 cp.pkt_type = htole16(0x00e0); /* HV1, HV2, HV3 */
164 err = hci_send_cmd(unit, HCI_CMD_ADD_SCO_CON, &cp, sizeof(cp));
166 hci_link_free(sco, err);
173 pcb->sp_mtu = unit->hci_max_sco_size;
178 * sco_peeraddr(pcb, sockaddr)
180 * Copy remote address of SCO pcb to sockaddr
183 sco_peeraddr(struct sco_pcb *pcb, struct sockaddr_bt *addr)
186 memset(addr, 0, sizeof(struct sockaddr_bt));
187 addr->bt_len = sizeof(struct sockaddr_bt);
188 addr->bt_family = AF_BLUETOOTH;
189 bdaddr_copy(&addr->bt_bdaddr, &pcb->sp_raddr);
194 * sco_disconnect(pcb, linger)
196 * Initiate disconnection of connected SCO pcb
199 sco_disconnect(struct sco_pcb *pcb, int linger)
202 struct hci_link *sco;
209 cp.con_handle = htole16(sco->hl_handle);
210 cp.reason = 0x13; /* "Remote User Terminated Connection" */
212 err = hci_send_cmd(sco->hl_unit, HCI_CMD_DISCONNECT, &cp, sizeof(cp));
213 if (err || linger == 0) {
216 hci_link_free(sco, err);
225 * Detach SCO pcb from handle and clear up
228 sco_detach(struct sco_pcb **handle)
232 KKASSERT(handle != NULL);
239 if (pcb->sp_link != NULL) {
240 sco_disconnect(pcb, 0);
244 LIST_REMOVE(pcb, sp_next);
245 kfree(pcb, M_BLUETOOTH);
252 * Mark pcb as a listener.
255 sco_listen(struct sco_pcb *pcb)
258 if (pcb->sp_link != NULL)
261 pcb->sp_flags |= SP_LISTENING;
266 * sco_send(pcb, mbuf)
268 * Send data on SCO pcb.
270 * Gross hackage, we just output the packet directly onto the unit queue.
271 * This will work fine for one channel per unit, but for more channels it
272 * really needs fixing. We set the context so that when the packet is sent,
273 * we can drop a record from the socket buffer.
276 sco_send(struct sco_pcb *pcb, struct mbuf *m)
278 hci_scodata_hdr_t *hdr;
281 if (pcb->sp_link == NULL) {
286 plen = m->m_pkthdr.len;
287 DPRINTFN(10, "%d bytes\n", plen);
290 * This is a temporary limitation, as USB devices cannot
291 * handle SCO packet sizes that are not an integer number
292 * of Isochronous frames. See ubt(4)
294 if (plen != pcb->sp_mtu) {
299 M_PREPEND(m, sizeof(hci_scodata_hdr_t), M_NOWAIT);
303 hdr = mtod(m, hci_scodata_hdr_t *);
304 hdr->type = HCI_SCO_DATA_PKT;
305 hdr->con_handle = htole16(pcb->sp_link->hl_handle);
309 M_SETCTX(m, pcb->sp_link);
310 hci_output_sco(pcb->sp_link->hl_unit, m);
316 * sco_setopt(pcb, option, addr)
318 * Set SCO pcb options
321 sco_setopt(struct sco_pcb *pcb, int opt, void *addr)
335 * sco_getopt(pcb, option, addr)
337 * Get SCO pcb options
340 sco_getopt(struct sco_pcb *pcb, int opt, void *addr)
345 *(uint16_t *)addr = pcb->sp_mtu;
346 return sizeof(uint16_t);
350 *(uint16_t *)addr = pcb->sp_link->hl_handle;
351 return sizeof(uint16_t);