summaryrefslogtreecommitdiff
path: root/sys/netbt/sco_upper.c
blob: 7a27b4a89cfe138829e95c1392417eaddcd35a2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*	$OpenBSD: sco_upper.c,v 1.2 2007/10/01 16:39:30 krw Exp $	*/
/*	$NetBSD: sco_upper.c,v 1.6 2007/03/30 20:47:03 plunky Exp $	*/

/*-
 * Copyright (c) 2006 Itronix Inc.
 * All rights reserved.
 *
 * Written by Iain Hibbert for Itronix Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of Itronix Inc. may not be used to endorse
 *    or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/systm.h>

#include <netbt/bluetooth.h>
#include <netbt/hci.h>
#include <netbt/sco.h>

/****************************************************************************
 *
 *	SCO - Upper Protocol API
 */

struct sco_pcb_list sco_pcb = LIST_HEAD_INITIALIZER(sco_pcb);

/*
 * sco_attach(handle, proto, upper)
 *
 *	Attach a new instance of SCO pcb to handle
 */
int
sco_attach(struct sco_pcb **handle,
		const struct btproto *proto, void *upper)
{
	struct sco_pcb *pcb;

	KASSERT(handle != NULL);
	KASSERT(proto != NULL);
	KASSERT(upper != NULL);

	pcb = malloc(sizeof(*pcb), M_BLUETOOTH, M_NOWAIT | M_ZERO);
	if (pcb == NULL)
		return ENOMEM;

	pcb->sp_proto = proto;
	pcb->sp_upper = upper;

	LIST_INSERT_HEAD(&sco_pcb, pcb, sp_next);

	*handle = pcb;
	return 0;
}

/*
 * sco_bind(pcb, sockaddr)
 *
 *	Bind SCO pcb to local address
 */
int
sco_bind(struct sco_pcb *pcb, struct sockaddr_bt *addr)
{

	bdaddr_copy(&pcb->sp_laddr, &addr->bt_bdaddr);
	return 0;
}

/*
 * sco_sockaddr(pcb, sockaddr)
 *
 *	Copy local address of PCB to sockaddr
 */
int
sco_sockaddr(struct sco_pcb *pcb, struct sockaddr_bt *addr)
{

	memset(addr, 0, sizeof(struct sockaddr_bt));
	addr->bt_len = sizeof(struct sockaddr_bt);
	addr->bt_family = AF_BLUETOOTH;
	bdaddr_copy(&addr->bt_bdaddr, &pcb->sp_laddr);
	return 0;
}

/*
 * sco_connect(pcb, sockaddr)
 *
 *	Initiate a SCO connection to the destination address.
 */
int
sco_connect(struct sco_pcb *pcb, struct sockaddr_bt *dest)
{
	hci_add_sco_con_cp cp;
	struct hci_unit *unit;
	struct hci_link *acl, *sco;
	int err;

	if (pcb->sp_flags & SP_LISTENING)
		return EINVAL;

	bdaddr_copy(&pcb->sp_raddr, &dest->bt_bdaddr);

	if (bdaddr_any(&pcb->sp_raddr))
		return EDESTADDRREQ;

	if (bdaddr_any(&pcb->sp_laddr)) {
		err = hci_route_lookup(&pcb->sp_laddr, &pcb->sp_raddr);
		if (err)
			return err;
	}

	unit = hci_unit_lookup(&pcb->sp_laddr);
	if (unit == NULL)
		return ENETDOWN;

	/*
	 * We must have an already open ACL connection before we open the SCO
	 * connection, and since SCO connections dont happen on their own we
	 * will not open one, the application wanting this should have opened
	 * it previously.
	 */
	acl = hci_link_lookup_bdaddr(unit, &pcb->sp_raddr, HCI_LINK_ACL);
	if (acl == NULL || acl->hl_state != HCI_LINK_OPEN)
		return EHOSTUNREACH;

	sco = hci_link_alloc(unit);
	if (sco == NULL)
		return ENOMEM;

	sco->hl_type = HCI_LINK_SCO;
	bdaddr_copy(&sco->hl_bdaddr, &pcb->sp_raddr);

	sco->hl_link = hci_acl_open(unit, &pcb->sp_raddr);
	KASSERT(sco->hl_link == acl);

	cp.con_handle = htole16(acl->hl_handle);
	cp.pkt_type = htole16(0x00e0);		/* HV1, HV2, HV3 */
	err = hci_send_cmd(unit, HCI_CMD_ADD_SCO_CON, &cp, sizeof(cp));
	if (err) {
		hci_link_free(sco, err);
		return err;
	}

	sco->hl_sco = pcb;
	pcb->sp_link = sco;

	pcb->sp_mtu = unit->hci_max_sco_size;
	return 0;
}

/*
 * sco_peeraddr(pcb, sockaddr)
 *
 *	Copy remote address of SCO pcb to sockaddr
 */
int
sco_peeraddr(struct sco_pcb *pcb, struct sockaddr_bt *addr)
{

	memset(addr, 0, sizeof(struct sockaddr_bt));
	addr->bt_len = sizeof(struct sockaddr_bt);
	addr->bt_family = AF_BLUETOOTH;
	bdaddr_copy(&addr->bt_bdaddr, &pcb->sp_raddr);
	return 0;
}

/*
 * sco_disconnect(pcb, linger)
 *
 *	Initiate disconnection of connected SCO pcb
 */
int
sco_disconnect(struct sco_pcb *pcb, int linger)
{
	hci_discon_cp cp;
	struct hci_link *sco;
	int err;

	sco = pcb->sp_link;
	if (sco == NULL)
		return EINVAL;

	cp.con_handle = htole16(sco->hl_handle);
	cp.reason = 0x13;	/* "Remote User Terminated Connection" */

	err = hci_send_cmd(sco->hl_unit, HCI_CMD_DISCONNECT, &cp, sizeof(cp));
	if (err || linger == 0) {
		sco->hl_sco = NULL;
		pcb->sp_link = NULL;
		hci_link_free(sco, err);
	}

	return err;
}

/*
 * sco_detach(handle)
 *
 *	Detach SCO pcb from handle and clear up
 */
int
sco_detach(struct sco_pcb **handle)
{
	struct sco_pcb *pcb;

	KASSERT(handle != NULL);
	pcb = *handle;
	*handle = NULL;

	if (pcb == NULL)
		return EINVAL;

	if (pcb->sp_link != NULL) {
		sco_disconnect(pcb, 0);
		pcb->sp_link = NULL;
	}

	LIST_REMOVE(pcb, sp_next);
	free(pcb, M_BLUETOOTH);
	return 0;
}

/*
 * sco_listen(pcb)
 *
 *	Mark pcb as a listener.
 */
int
sco_listen(struct sco_pcb *pcb)
{

	if (pcb->sp_link != NULL)
		return EINVAL;

	pcb->sp_flags |= SP_LISTENING;
	return 0;
}

/*
 * sco_send(pcb, mbuf)
 *
 *	Send data on SCO pcb.
 *
 * Gross hackage, we just output the packet directly onto the unit queue.
 * This will work fine for one channel per unit, but for more channels it
 * really needs fixing. We set the context so that when the packet is sent,
 * we can drop a record from the socket buffer.
 */
int
sco_send(struct sco_pcb *pcb, struct mbuf *m)
{
	hci_scodata_hdr_t *hdr;
	int plen;

	if (pcb->sp_link == NULL) {
		m_freem(m);
		return EINVAL;
	}

	plen = m->m_pkthdr.len;
	DPRINTFN(10, "%d bytes\n", plen);

	/*
	 * This is a temporary limitation, as USB devices cannot
	 * handle SCO packet sizes that are not an integer number
	 * of Isochronous frames. See ubt(4)
	 */
	if (plen != pcb->sp_mtu) {
		m_freem(m);
		return EMSGSIZE;
	}

	M_PREPEND(m, sizeof(hci_scodata_hdr_t), M_DONTWAIT);
	if (m == NULL)
		return ENOMEM;

	hdr = mtod(m, hci_scodata_hdr_t *);
	hdr->type = HCI_SCO_DATA_PKT;
	hdr->con_handle = htole16(pcb->sp_link->hl_handle);
	hdr->length = plen;

	pcb->sp_pending++;
	M_SETCTX(m, pcb->sp_link);
	hci_output_sco(pcb->sp_link->hl_unit, m);

	return 0;
}

/*
 * sco_setopt(pcb, option, addr)
 *
 *	Set SCO pcb options
 */
int
sco_setopt(struct sco_pcb *pcb, int opt, void *addr)
{
	int err = 0;

	switch (opt) {
	default:
		err = ENOPROTOOPT;
		break;
	}

	return err;
}

/*
 * sco_getopt(pcb, option, addr)
 *
 *	Get SCO pcb options
 */
int
sco_getopt(struct sco_pcb *pcb, int opt, void *addr)
{

	switch (opt) {
	case SO_SCO_MTU:
		*(uint16_t *)addr = pcb->sp_mtu;
		return sizeof(uint16_t);

	case SO_SCO_HANDLE:
		if (pcb->sp_link) {
			*(uint16_t *)addr = pcb->sp_link->hl_handle;
			return sizeof(uint16_t);
		}
		break;

	default:
		break;
	}
	return 0;
}