1 /* $NetBSD: sco_upper.c,v 1.8 2008/08/06 15:01:24 plunky Exp $ */
4 * Copyright (c) 2006 Itronix Inc.
7 * Written by Iain Hibbert for Itronix Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: sco_upper.c,v 1.8 2008/08/06 15:01:24 plunky Exp $");
37 #include <sys/param.h>
38 #include <sys/kernel.h>
41 #include <sys/socketvar.h>
42 #include <sys/systm.h>
44 #include <netbt/bluetooth.h>
45 #include <netbt/hci.h>
46 #include <netbt/sco.h>
48 /****************************************************************************
50 * SCO - Upper Protocol API
53 struct sco_pcb_list sco_pcb
= LIST_HEAD_INITIALIZER(sco_pcb
);
56 * sco_attach(handle, proto, upper)
58 * Attach a new instance of SCO pcb to handle
61 sco_attach(struct sco_pcb
**handle
,
62 const struct btproto
*proto
, void *upper
)
66 KASSERT(handle
!= NULL
);
67 KASSERT(proto
!= NULL
);
68 KASSERT(upper
!= NULL
);
70 pcb
= malloc(sizeof(struct sco_pcb
), M_BLUETOOTH
,
75 pcb
->sp_proto
= proto
;
76 pcb
->sp_upper
= upper
;
78 LIST_INSERT_HEAD(&sco_pcb
, pcb
, sp_next
);
85 * sco_bind(pcb, sockaddr)
87 * Bind SCO pcb to local address
90 sco_bind(struct sco_pcb
*pcb
, struct sockaddr_bt
*addr
)
93 if (pcb
->sp_link
!= NULL
|| pcb
->sp_flags
& SP_LISTENING
)
96 bdaddr_copy(&pcb
->sp_laddr
, &addr
->bt_bdaddr
);
101 * sco_sockaddr(pcb, sockaddr)
103 * Copy local address of PCB to sockaddr
106 sco_sockaddr(struct sco_pcb
*pcb
, struct sockaddr_bt
*addr
)
109 memset(addr
, 0, sizeof(struct sockaddr_bt
));
110 addr
->bt_len
= sizeof(struct sockaddr_bt
);
111 addr
->bt_family
= AF_BLUETOOTH
;
112 bdaddr_copy(&addr
->bt_bdaddr
, &pcb
->sp_laddr
);
117 * sco_connect(pcb, sockaddr)
119 * Initiate a SCO connection to the destination address.
122 sco_connect(struct sco_pcb
*pcb
, struct sockaddr_bt
*dest
)
124 hci_add_sco_con_cp cp
;
125 struct hci_unit
*unit
;
126 struct hci_link
*acl
, *sco
;
129 if (pcb
->sp_flags
& SP_LISTENING
)
132 bdaddr_copy(&pcb
->sp_raddr
, &dest
->bt_bdaddr
);
134 if (bdaddr_any(&pcb
->sp_raddr
))
137 if (bdaddr_any(&pcb
->sp_laddr
)) {
138 err
= hci_route_lookup(&pcb
->sp_laddr
, &pcb
->sp_raddr
);
143 unit
= hci_unit_lookup(&pcb
->sp_laddr
);
148 * We must have an already open ACL connection before we open the SCO
149 * connection, and since SCO connections dont happen on their own we
150 * will not open one, the application wanting this should have opened
153 acl
= hci_link_lookup_bdaddr(unit
, &pcb
->sp_raddr
, HCI_LINK_ACL
);
154 if (acl
== NULL
|| acl
->hl_state
!= HCI_LINK_OPEN
)
157 sco
= hci_link_alloc(unit
, &pcb
->sp_raddr
, HCI_LINK_SCO
);
161 sco
->hl_link
= hci_acl_open(unit
, &pcb
->sp_raddr
);
162 KASSERT(sco
->hl_link
== acl
);
164 cp
.con_handle
= htole16(acl
->hl_handle
);
165 cp
.pkt_type
= htole16(0x00e0); /* HV1, HV2, HV3 */
166 err
= hci_send_cmd(unit
, HCI_CMD_ADD_SCO_CON
, &cp
, sizeof(cp
));
168 hci_link_free(sco
, err
);
175 pcb
->sp_mtu
= unit
->hci_max_sco_size
;
180 * sco_peeraddr(pcb, sockaddr)
182 * Copy remote address of SCO pcb to sockaddr
185 sco_peeraddr(struct sco_pcb
*pcb
, struct sockaddr_bt
*addr
)
188 memset(addr
, 0, sizeof(struct sockaddr_bt
));
189 addr
->bt_len
= sizeof(struct sockaddr_bt
);
190 addr
->bt_family
= AF_BLUETOOTH
;
191 bdaddr_copy(&addr
->bt_bdaddr
, &pcb
->sp_raddr
);
196 * sco_disconnect(pcb, linger)
198 * Initiate disconnection of connected SCO pcb
201 sco_disconnect(struct sco_pcb
*pcb
, int linger
)
204 struct hci_link
*sco
;
211 cp
.con_handle
= htole16(sco
->hl_handle
);
212 cp
.reason
= 0x13; /* "Remote User Terminated Connection" */
214 err
= hci_send_cmd(sco
->hl_unit
, HCI_CMD_DISCONNECT
, &cp
, sizeof(cp
));
215 if (err
|| linger
== 0) {
218 hci_link_free(sco
, err
);
227 * Detach SCO pcb from handle and clear up
230 sco_detach(struct sco_pcb
**handle
)
234 KASSERT(handle
!= NULL
);
241 if (pcb
->sp_link
!= NULL
) {
242 sco_disconnect(pcb
, 0);
246 LIST_REMOVE(pcb
, sp_next
);
247 free(pcb
, M_BLUETOOTH
);
254 * Mark pcb as a listener.
257 sco_listen(struct sco_pcb
*pcb
)
260 if (pcb
->sp_link
!= NULL
)
263 pcb
->sp_flags
|= SP_LISTENING
;
268 * sco_send(pcb, mbuf)
270 * Send data on SCO pcb.
272 * Gross hackage, we just output the packet directly onto the unit queue.
273 * This will work fine for one channel per unit, but for more channels it
274 * really needs fixing. We set the context so that when the packet is sent,
275 * we can drop a record from the socket buffer.
278 sco_send(struct sco_pcb
*pcb
, struct mbuf
*m
)
280 hci_scodata_hdr_t
*hdr
;
283 if (pcb
->sp_link
== NULL
) {
288 plen
= m
->m_pkthdr
.len
;
289 DPRINTFN(10, "%d bytes\n", plen
);
292 * This is a temporary limitation, as USB devices cannot
293 * handle SCO packet sizes that are not an integer number
294 * of Isochronous frames. See ubt(4)
296 if (plen
!= pcb
->sp_mtu
) {
301 M_PREPEND(m
, sizeof(hci_scodata_hdr_t
), M_DONTWAIT
);
305 hdr
= mtod(m
, hci_scodata_hdr_t
*);
306 hdr
->type
= HCI_SCO_DATA_PKT
;
307 hdr
->con_handle
= htole16(pcb
->sp_link
->hl_handle
);
311 M_SETCTX(m
, pcb
->sp_link
);
312 hci_output_sco(pcb
->sp_link
->hl_unit
, m
);
318 * sco_setopt(pcb, sopt)
320 * Set SCO pcb options
323 sco_setopt(struct sco_pcb
*pcb
, const struct sockopt
*sopt
)
327 switch (sopt
->sopt_name
) {
337 * sco_getopt(pcb, sopt)
339 * Get SCO pcb options
342 sco_getopt(struct sco_pcb
*pcb
, struct sockopt
*sopt
)
345 switch (sopt
->sopt_name
) {
347 return sockopt_set(sopt
, &pcb
->sp_mtu
, sizeof(uint16_t));
351 return sockopt_set(sopt
,
352 &pcb
->sp_link
->hl_handle
, sizeof(uint16_t));