sco_upper.c revision 1.1.2.4 1 1.1.2.4 yamt /* $NetBSD: sco_upper.c,v 1.1.2.4 2007/09/03 14:42:44 yamt Exp $ */
2 1.1.2.2 yamt
3 1.1.2.2 yamt /*-
4 1.1.2.2 yamt * Copyright (c) 2006 Itronix Inc.
5 1.1.2.2 yamt * All rights reserved.
6 1.1.2.2 yamt *
7 1.1.2.2 yamt * Written by Iain Hibbert for Itronix Inc.
8 1.1.2.2 yamt *
9 1.1.2.2 yamt * Redistribution and use in source and binary forms, with or without
10 1.1.2.2 yamt * modification, are permitted provided that the following conditions
11 1.1.2.2 yamt * are met:
12 1.1.2.2 yamt * 1. Redistributions of source code must retain the above copyright
13 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer.
14 1.1.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
15 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer in the
16 1.1.2.2 yamt * documentation and/or other materials provided with the distribution.
17 1.1.2.2 yamt * 3. The name of Itronix Inc. may not be used to endorse
18 1.1.2.2 yamt * or promote products derived from this software without specific
19 1.1.2.2 yamt * prior written permission.
20 1.1.2.2 yamt *
21 1.1.2.2 yamt * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 1.1.2.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.1.2.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1.2.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 1.1.2.2 yamt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 1.1.2.2 yamt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.1.2.2 yamt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 1.1.2.2 yamt * ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1.2.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1.2.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1.2.2 yamt * POSSIBILITY OF SUCH DAMAGE.
32 1.1.2.2 yamt */
33 1.1.2.2 yamt
34 1.1.2.2 yamt #include <sys/cdefs.h>
35 1.1.2.4 yamt __KERNEL_RCSID(0, "$NetBSD: sco_upper.c,v 1.1.2.4 2007/09/03 14:42:44 yamt Exp $");
36 1.1.2.2 yamt
37 1.1.2.2 yamt #include <sys/param.h>
38 1.1.2.2 yamt #include <sys/kernel.h>
39 1.1.2.2 yamt #include <sys/mbuf.h>
40 1.1.2.2 yamt #include <sys/proc.h>
41 1.1.2.2 yamt #include <sys/systm.h>
42 1.1.2.2 yamt
43 1.1.2.2 yamt #include <netbt/bluetooth.h>
44 1.1.2.2 yamt #include <netbt/hci.h>
45 1.1.2.2 yamt #include <netbt/sco.h>
46 1.1.2.2 yamt
47 1.1.2.2 yamt /****************************************************************************
48 1.1.2.2 yamt *
49 1.1.2.2 yamt * SCO - Upper Protocol API
50 1.1.2.2 yamt */
51 1.1.2.2 yamt
52 1.1.2.2 yamt struct sco_pcb_list sco_pcb = LIST_HEAD_INITIALIZER(sco_pcb);
53 1.1.2.2 yamt
54 1.1.2.2 yamt /*
55 1.1.2.2 yamt * sco_attach(handle, proto, upper)
56 1.1.2.2 yamt *
57 1.1.2.2 yamt * Attach a new instance of SCO pcb to handle
58 1.1.2.2 yamt */
59 1.1.2.2 yamt int
60 1.1.2.2 yamt sco_attach(struct sco_pcb **handle,
61 1.1.2.2 yamt const struct btproto *proto, void *upper)
62 1.1.2.2 yamt {
63 1.1.2.2 yamt struct sco_pcb *pcb;
64 1.1.2.2 yamt
65 1.1.2.4 yamt KASSERT(handle != NULL);
66 1.1.2.4 yamt KASSERT(proto != NULL);
67 1.1.2.4 yamt KASSERT(upper != NULL);
68 1.1.2.2 yamt
69 1.1.2.2 yamt pcb = malloc(sizeof(struct sco_pcb), M_BLUETOOTH,
70 1.1.2.2 yamt M_NOWAIT | M_ZERO);
71 1.1.2.2 yamt if (pcb == NULL)
72 1.1.2.2 yamt return ENOMEM;
73 1.1.2.2 yamt
74 1.1.2.2 yamt pcb->sp_proto = proto;
75 1.1.2.2 yamt pcb->sp_upper = upper;
76 1.1.2.2 yamt
77 1.1.2.2 yamt LIST_INSERT_HEAD(&sco_pcb, pcb, sp_next);
78 1.1.2.2 yamt
79 1.1.2.2 yamt *handle = pcb;
80 1.1.2.2 yamt return 0;
81 1.1.2.2 yamt }
82 1.1.2.2 yamt
83 1.1.2.2 yamt /*
84 1.1.2.2 yamt * sco_bind(pcb, sockaddr)
85 1.1.2.2 yamt *
86 1.1.2.2 yamt * Bind SCO pcb to local address
87 1.1.2.2 yamt */
88 1.1.2.2 yamt int
89 1.1.2.2 yamt sco_bind(struct sco_pcb *pcb, struct sockaddr_bt *addr)
90 1.1.2.2 yamt {
91 1.1.2.2 yamt
92 1.1.2.2 yamt bdaddr_copy(&pcb->sp_laddr, &addr->bt_bdaddr);
93 1.1.2.2 yamt return 0;
94 1.1.2.2 yamt }
95 1.1.2.2 yamt
96 1.1.2.2 yamt /*
97 1.1.2.2 yamt * sco_sockaddr(pcb, sockaddr)
98 1.1.2.2 yamt *
99 1.1.2.2 yamt * Copy local address of PCB to sockaddr
100 1.1.2.2 yamt */
101 1.1.2.2 yamt int
102 1.1.2.2 yamt sco_sockaddr(struct sco_pcb *pcb, struct sockaddr_bt *addr)
103 1.1.2.2 yamt {
104 1.1.2.2 yamt
105 1.1.2.2 yamt memset(addr, 0, sizeof(struct sockaddr_bt));
106 1.1.2.2 yamt addr->bt_len = sizeof(struct sockaddr_bt);
107 1.1.2.2 yamt addr->bt_family = AF_BLUETOOTH;
108 1.1.2.2 yamt bdaddr_copy(&addr->bt_bdaddr, &pcb->sp_laddr);
109 1.1.2.2 yamt return 0;
110 1.1.2.2 yamt }
111 1.1.2.2 yamt
112 1.1.2.2 yamt /*
113 1.1.2.2 yamt * sco_connect(pcb, sockaddr)
114 1.1.2.2 yamt *
115 1.1.2.2 yamt * Initiate a SCO connection to the destination address.
116 1.1.2.2 yamt */
117 1.1.2.2 yamt int
118 1.1.2.2 yamt sco_connect(struct sco_pcb *pcb, struct sockaddr_bt *dest)
119 1.1.2.2 yamt {
120 1.1.2.2 yamt hci_add_sco_con_cp cp;
121 1.1.2.2 yamt struct hci_unit *unit;
122 1.1.2.2 yamt struct hci_link *acl, *sco;
123 1.1.2.2 yamt int err;
124 1.1.2.2 yamt
125 1.1.2.2 yamt if (pcb->sp_flags & SP_LISTENING)
126 1.1.2.2 yamt return EINVAL;
127 1.1.2.2 yamt
128 1.1.2.2 yamt bdaddr_copy(&pcb->sp_raddr, &dest->bt_bdaddr);
129 1.1.2.2 yamt
130 1.1.2.2 yamt if (bdaddr_any(&pcb->sp_raddr))
131 1.1.2.2 yamt return EDESTADDRREQ;
132 1.1.2.2 yamt
133 1.1.2.2 yamt if (bdaddr_any(&pcb->sp_laddr)) {
134 1.1.2.2 yamt err = hci_route_lookup(&pcb->sp_laddr, &pcb->sp_raddr);
135 1.1.2.2 yamt if (err)
136 1.1.2.2 yamt return err;
137 1.1.2.2 yamt }
138 1.1.2.2 yamt
139 1.1.2.2 yamt unit = hci_unit_lookup(&pcb->sp_laddr);
140 1.1.2.2 yamt if (unit == NULL)
141 1.1.2.2 yamt return ENETDOWN;
142 1.1.2.2 yamt
143 1.1.2.2 yamt /*
144 1.1.2.2 yamt * We must have an already open ACL connection before we open the SCO
145 1.1.2.2 yamt * connection, and since SCO connections dont happen on their own we
146 1.1.2.2 yamt * will not open one, the application wanting this should have opened
147 1.1.2.2 yamt * it previously.
148 1.1.2.2 yamt */
149 1.1.2.2 yamt acl = hci_link_lookup_bdaddr(unit, &pcb->sp_raddr, HCI_LINK_ACL);
150 1.1.2.2 yamt if (acl == NULL || acl->hl_state != HCI_LINK_OPEN)
151 1.1.2.2 yamt return EHOSTUNREACH;
152 1.1.2.2 yamt
153 1.1.2.2 yamt sco = hci_link_alloc(unit);
154 1.1.2.2 yamt if (sco == NULL)
155 1.1.2.2 yamt return ENOMEM;
156 1.1.2.2 yamt
157 1.1.2.2 yamt sco->hl_type = HCI_LINK_SCO;
158 1.1.2.2 yamt bdaddr_copy(&sco->hl_bdaddr, &pcb->sp_raddr);
159 1.1.2.2 yamt
160 1.1.2.2 yamt sco->hl_link = hci_acl_open(unit, &pcb->sp_raddr);
161 1.1.2.2 yamt KASSERT(sco->hl_link == acl);
162 1.1.2.2 yamt
163 1.1.2.2 yamt cp.con_handle = htole16(acl->hl_handle);
164 1.1.2.2 yamt cp.pkt_type = htole16(0x00e0); /* HV1, HV2, HV3 */
165 1.1.2.2 yamt err = hci_send_cmd(unit, HCI_CMD_ADD_SCO_CON, &cp, sizeof(cp));
166 1.1.2.2 yamt if (err) {
167 1.1.2.2 yamt hci_link_free(sco, err);
168 1.1.2.2 yamt return err;
169 1.1.2.2 yamt }
170 1.1.2.2 yamt
171 1.1.2.2 yamt sco->hl_sco = pcb;
172 1.1.2.2 yamt pcb->sp_link = sco;
173 1.1.2.2 yamt
174 1.1.2.2 yamt pcb->sp_mtu = unit->hci_max_sco_size;
175 1.1.2.2 yamt return 0;
176 1.1.2.2 yamt }
177 1.1.2.2 yamt
178 1.1.2.2 yamt /*
179 1.1.2.2 yamt * sco_peeraddr(pcb, sockaddr)
180 1.1.2.2 yamt *
181 1.1.2.2 yamt * Copy remote address of SCO pcb to sockaddr
182 1.1.2.2 yamt */
183 1.1.2.2 yamt int
184 1.1.2.2 yamt sco_peeraddr(struct sco_pcb *pcb, struct sockaddr_bt *addr)
185 1.1.2.2 yamt {
186 1.1.2.2 yamt
187 1.1.2.2 yamt memset(addr, 0, sizeof(struct sockaddr_bt));
188 1.1.2.2 yamt addr->bt_len = sizeof(struct sockaddr_bt);
189 1.1.2.2 yamt addr->bt_family = AF_BLUETOOTH;
190 1.1.2.2 yamt bdaddr_copy(&addr->bt_bdaddr, &pcb->sp_raddr);
191 1.1.2.2 yamt return 0;
192 1.1.2.2 yamt }
193 1.1.2.2 yamt
194 1.1.2.2 yamt /*
195 1.1.2.2 yamt * sco_disconnect(pcb, linger)
196 1.1.2.2 yamt *
197 1.1.2.2 yamt * Initiate disconnection of connected SCO pcb
198 1.1.2.2 yamt */
199 1.1.2.2 yamt int
200 1.1.2.2 yamt sco_disconnect(struct sco_pcb *pcb, int linger)
201 1.1.2.2 yamt {
202 1.1.2.2 yamt hci_discon_cp cp;
203 1.1.2.2 yamt struct hci_link *sco;
204 1.1.2.2 yamt int err;
205 1.1.2.2 yamt
206 1.1.2.2 yamt sco = pcb->sp_link;
207 1.1.2.2 yamt if (sco == NULL)
208 1.1.2.2 yamt return EINVAL;
209 1.1.2.2 yamt
210 1.1.2.2 yamt cp.con_handle = htole16(sco->hl_handle);
211 1.1.2.2 yamt cp.reason = 0x13; /* "Remote User Terminated Connection" */
212 1.1.2.2 yamt
213 1.1.2.2 yamt err = hci_send_cmd(sco->hl_unit, HCI_CMD_DISCONNECT, &cp, sizeof(cp));
214 1.1.2.2 yamt if (err || linger == 0) {
215 1.1.2.2 yamt sco->hl_sco = NULL;
216 1.1.2.2 yamt pcb->sp_link = NULL;
217 1.1.2.2 yamt hci_link_free(sco, err);
218 1.1.2.2 yamt }
219 1.1.2.2 yamt
220 1.1.2.2 yamt return err;
221 1.1.2.2 yamt }
222 1.1.2.2 yamt
223 1.1.2.2 yamt /*
224 1.1.2.2 yamt * sco_detach(handle)
225 1.1.2.2 yamt *
226 1.1.2.2 yamt * Detach SCO pcb from handle and clear up
227 1.1.2.2 yamt */
228 1.1.2.2 yamt int
229 1.1.2.2 yamt sco_detach(struct sco_pcb **handle)
230 1.1.2.2 yamt {
231 1.1.2.2 yamt struct sco_pcb *pcb;
232 1.1.2.2 yamt
233 1.1.2.4 yamt KASSERT(handle != NULL);
234 1.1.2.2 yamt pcb = *handle;
235 1.1.2.2 yamt *handle = NULL;
236 1.1.2.2 yamt
237 1.1.2.2 yamt if (pcb == NULL)
238 1.1.2.2 yamt return EINVAL;
239 1.1.2.2 yamt
240 1.1.2.2 yamt if (pcb->sp_link != NULL) {
241 1.1.2.2 yamt sco_disconnect(pcb, 0);
242 1.1.2.2 yamt pcb->sp_link = NULL;
243 1.1.2.2 yamt }
244 1.1.2.2 yamt
245 1.1.2.2 yamt LIST_REMOVE(pcb, sp_next);
246 1.1.2.2 yamt free(pcb, M_BLUETOOTH);
247 1.1.2.2 yamt return 0;
248 1.1.2.2 yamt }
249 1.1.2.2 yamt
250 1.1.2.2 yamt /*
251 1.1.2.2 yamt * sco_listen(pcb)
252 1.1.2.2 yamt *
253 1.1.2.2 yamt * Mark pcb as a listener.
254 1.1.2.2 yamt */
255 1.1.2.2 yamt int
256 1.1.2.2 yamt sco_listen(struct sco_pcb *pcb)
257 1.1.2.2 yamt {
258 1.1.2.2 yamt
259 1.1.2.2 yamt if (pcb->sp_link != NULL)
260 1.1.2.2 yamt return EINVAL;
261 1.1.2.2 yamt
262 1.1.2.2 yamt pcb->sp_flags |= SP_LISTENING;
263 1.1.2.2 yamt return 0;
264 1.1.2.2 yamt }
265 1.1.2.2 yamt
266 1.1.2.2 yamt /*
267 1.1.2.2 yamt * sco_send(pcb, mbuf)
268 1.1.2.2 yamt *
269 1.1.2.2 yamt * Send data on SCO pcb.
270 1.1.2.2 yamt *
271 1.1.2.2 yamt * Gross hackage, we just output the packet directly onto the unit queue.
272 1.1.2.2 yamt * This will work fine for one channel per unit, but for more channels it
273 1.1.2.2 yamt * really needs fixing. We set the context so that when the packet is sent,
274 1.1.2.2 yamt * we can drop a record from the socket buffer.
275 1.1.2.2 yamt */
276 1.1.2.2 yamt int
277 1.1.2.2 yamt sco_send(struct sco_pcb *pcb, struct mbuf *m)
278 1.1.2.2 yamt {
279 1.1.2.2 yamt hci_scodata_hdr_t *hdr;
280 1.1.2.2 yamt int plen;
281 1.1.2.2 yamt
282 1.1.2.2 yamt if (pcb->sp_link == NULL) {
283 1.1.2.2 yamt m_freem(m);
284 1.1.2.2 yamt return EINVAL;
285 1.1.2.2 yamt }
286 1.1.2.2 yamt
287 1.1.2.2 yamt plen = m->m_pkthdr.len;
288 1.1.2.2 yamt DPRINTFN(10, "%d bytes\n", plen);
289 1.1.2.2 yamt
290 1.1.2.3 yamt /*
291 1.1.2.3 yamt * This is a temporary limitation, as USB devices cannot
292 1.1.2.3 yamt * handle SCO packet sizes that are not an integer number
293 1.1.2.3 yamt * of Isochronous frames. See ubt(4)
294 1.1.2.3 yamt */
295 1.1.2.3 yamt if (plen != pcb->sp_mtu) {
296 1.1.2.3 yamt m_freem(m);
297 1.1.2.3 yamt return EMSGSIZE;
298 1.1.2.3 yamt }
299 1.1.2.3 yamt
300 1.1.2.2 yamt M_PREPEND(m, sizeof(hci_scodata_hdr_t), M_DONTWAIT);
301 1.1.2.2 yamt if (m == NULL)
302 1.1.2.2 yamt return ENOMEM;
303 1.1.2.2 yamt
304 1.1.2.2 yamt hdr = mtod(m, hci_scodata_hdr_t *);
305 1.1.2.2 yamt hdr->type = HCI_SCO_DATA_PKT;
306 1.1.2.2 yamt hdr->con_handle = htole16(pcb->sp_link->hl_handle);
307 1.1.2.2 yamt hdr->length = plen;
308 1.1.2.2 yamt
309 1.1.2.2 yamt pcb->sp_pending++;
310 1.1.2.2 yamt M_SETCTX(m, pcb->sp_link);
311 1.1.2.2 yamt hci_output_sco(pcb->sp_link->hl_unit, m);
312 1.1.2.2 yamt
313 1.1.2.2 yamt return 0;
314 1.1.2.2 yamt }
315 1.1.2.2 yamt
316 1.1.2.2 yamt /*
317 1.1.2.2 yamt * sco_setopt(pcb, option, addr)
318 1.1.2.2 yamt *
319 1.1.2.2 yamt * Set SCO pcb options
320 1.1.2.2 yamt */
321 1.1.2.2 yamt int
322 1.1.2.2 yamt sco_setopt(struct sco_pcb *pcb, int opt, void *addr)
323 1.1.2.2 yamt {
324 1.1.2.2 yamt int err = 0;
325 1.1.2.2 yamt
326 1.1.2.2 yamt switch (opt) {
327 1.1.2.2 yamt default:
328 1.1.2.4 yamt err = ENOPROTOOPT;
329 1.1.2.2 yamt break;
330 1.1.2.2 yamt }
331 1.1.2.2 yamt
332 1.1.2.2 yamt return err;
333 1.1.2.2 yamt }
334 1.1.2.2 yamt
335 1.1.2.2 yamt /*
336 1.1.2.2 yamt * sco_getopt(pcb, option, addr)
337 1.1.2.2 yamt *
338 1.1.2.2 yamt * Get SCO pcb options
339 1.1.2.2 yamt */
340 1.1.2.2 yamt int
341 1.1.2.2 yamt sco_getopt(struct sco_pcb *pcb, int opt, void *addr)
342 1.1.2.2 yamt {
343 1.1.2.2 yamt
344 1.1.2.2 yamt switch (opt) {
345 1.1.2.2 yamt case SO_SCO_MTU:
346 1.1.2.2 yamt *(uint16_t *)addr = pcb->sp_mtu;
347 1.1.2.2 yamt return sizeof(uint16_t);
348 1.1.2.2 yamt
349 1.1.2.2 yamt case SO_SCO_HANDLE:
350 1.1.2.2 yamt if (pcb->sp_link) {
351 1.1.2.2 yamt *(uint16_t *)addr = pcb->sp_link->hl_handle;
352 1.1.2.2 yamt return sizeof(uint16_t);
353 1.1.2.2 yamt }
354 1.1.2.2 yamt break;
355 1.1.2.2 yamt
356 1.1.2.2 yamt default:
357 1.1.2.2 yamt break;
358 1.1.2.2 yamt }
359 1.1.2.2 yamt return 0;
360 1.1.2.2 yamt }
361