1 | /* $NetBSD: sco_upper.c,v 1.16 2014/08/05 07:55:32 rtr 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.16 2014/08/05 07:55:32 rtr 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/socketvar.h> |
42 | #include <sys/systm.h> |
43 | |
44 | #include <netbt/bluetooth.h> |
45 | #include <netbt/hci.h> |
46 | #include <netbt/sco.h> |
47 | |
48 | /**************************************************************************** |
49 | * |
50 | * SCO - Upper Protocol API |
51 | */ |
52 | |
53 | struct sco_pcb_list sco_pcb = LIST_HEAD_INITIALIZER(sco_pcb); |
54 | |
55 | /* |
56 | * sco_attach_pcb(handle, proto, upper) |
57 | * |
58 | * Attach a new instance of SCO pcb to handle |
59 | */ |
60 | int |
61 | sco_attach_pcb(struct sco_pcb **handle, |
62 | const struct btproto *proto, void *upper) |
63 | { |
64 | struct sco_pcb *pcb; |
65 | |
66 | KASSERT(handle != NULL); |
67 | KASSERT(proto != NULL); |
68 | KASSERT(upper != NULL); |
69 | |
70 | pcb = malloc(sizeof(struct sco_pcb), M_BLUETOOTH, |
71 | M_NOWAIT | M_ZERO); |
72 | if (pcb == NULL) |
73 | return ENOMEM; |
74 | |
75 | pcb->sp_proto = proto; |
76 | pcb->sp_upper = upper; |
77 | |
78 | LIST_INSERT_HEAD(&sco_pcb, pcb, sp_next); |
79 | |
80 | *handle = pcb; |
81 | return 0; |
82 | } |
83 | |
84 | /* |
85 | * sco_bind_pcb(pcb, sockaddr) |
86 | * |
87 | * Bind SCO pcb to local address |
88 | */ |
89 | int |
90 | sco_bind_pcb(struct sco_pcb *pcb, struct sockaddr_bt *addr) |
91 | { |
92 | |
93 | if (pcb->sp_link != NULL || pcb->sp_flags & SP_LISTENING) |
94 | return EINVAL; |
95 | |
96 | bdaddr_copy(&pcb->sp_laddr, &addr->bt_bdaddr); |
97 | return 0; |
98 | } |
99 | |
100 | /* |
101 | * sco_sockaddr_pcb(pcb, sockaddr) |
102 | * |
103 | * Copy local address of PCB to sockaddr |
104 | */ |
105 | int |
106 | sco_sockaddr_pcb(struct sco_pcb *pcb, struct sockaddr_bt *addr) |
107 | { |
108 | |
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); |
113 | return 0; |
114 | } |
115 | |
116 | /* |
117 | * sco_connect_pcb(pcb, sockaddr) |
118 | * |
119 | * Initiate a SCO connection to the destination address. |
120 | */ |
121 | int |
122 | sco_connect_pcb(struct sco_pcb *pcb, struct sockaddr_bt *dest) |
123 | { |
124 | hci_add_sco_con_cp cp; |
125 | struct hci_unit *unit; |
126 | struct hci_link *acl, *sco; |
127 | int err; |
128 | |
129 | if (pcb->sp_flags & SP_LISTENING) |
130 | return EINVAL; |
131 | |
132 | bdaddr_copy(&pcb->sp_raddr, &dest->bt_bdaddr); |
133 | |
134 | if (bdaddr_any(&pcb->sp_raddr)) |
135 | return EDESTADDRREQ; |
136 | |
137 | if (bdaddr_any(&pcb->sp_laddr)) { |
138 | err = hci_route_lookup(&pcb->sp_laddr, &pcb->sp_raddr); |
139 | if (err) |
140 | return err; |
141 | } |
142 | |
143 | unit = hci_unit_lookup(&pcb->sp_laddr); |
144 | if (unit == NULL) |
145 | return ENETDOWN; |
146 | |
147 | /* |
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 |
151 | * it previously. |
152 | */ |
153 | acl = hci_link_lookup_bdaddr(unit, &pcb->sp_raddr, HCI_LINK_ACL); |
154 | if (acl == NULL || acl->hl_state != HCI_LINK_OPEN) |
155 | return EHOSTUNREACH; |
156 | |
157 | sco = hci_link_alloc(unit, &pcb->sp_raddr, HCI_LINK_SCO); |
158 | if (sco == NULL) |
159 | return ENOMEM; |
160 | |
161 | sco->hl_link = hci_acl_open(unit, &pcb->sp_raddr); |
162 | KASSERT(sco->hl_link == acl); |
163 | |
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)); |
167 | if (err) { |
168 | hci_link_free(sco, err); |
169 | return err; |
170 | } |
171 | |
172 | sco->hl_sco = pcb; |
173 | pcb->sp_link = sco; |
174 | |
175 | pcb->sp_mtu = unit->hci_max_sco_size; |
176 | return 0; |
177 | } |
178 | |
179 | /* |
180 | * sco_peeraddr_pcb(pcb, sockaddr) |
181 | * |
182 | * Copy remote address of SCO pcb to sockaddr |
183 | */ |
184 | int |
185 | sco_peeraddr_pcb(struct sco_pcb *pcb, struct sockaddr_bt *addr) |
186 | { |
187 | |
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); |
192 | return 0; |
193 | } |
194 | |
195 | /* |
196 | * sco_disconnect_pcb(pcb, linger) |
197 | * |
198 | * Initiate disconnection of connected SCO pcb |
199 | */ |
200 | int |
201 | sco_disconnect_pcb(struct sco_pcb *pcb, int linger) |
202 | { |
203 | hci_discon_cp cp; |
204 | struct hci_link *sco; |
205 | int err; |
206 | |
207 | sco = pcb->sp_link; |
208 | if (sco == NULL) |
209 | return EINVAL; |
210 | |
211 | cp.con_handle = htole16(sco->hl_handle); |
212 | cp.reason = 0x13; /* "Remote User Terminated Connection" */ |
213 | |
214 | err = hci_send_cmd(sco->hl_unit, HCI_CMD_DISCONNECT, &cp, sizeof(cp)); |
215 | if (err || linger == 0) { |
216 | sco->hl_sco = NULL; |
217 | pcb->sp_link = NULL; |
218 | hci_link_free(sco, err); |
219 | } |
220 | |
221 | return err; |
222 | } |
223 | |
224 | /* |
225 | * sco_detach_pcb(handle) |
226 | * |
227 | * Detach SCO pcb from handle and clear up |
228 | */ |
229 | void |
230 | sco_detach_pcb(struct sco_pcb **handle) |
231 | { |
232 | struct sco_pcb *pcb; |
233 | |
234 | KASSERT(handle != NULL); |
235 | pcb = *handle; |
236 | *handle = NULL; |
237 | |
238 | if (pcb->sp_link != NULL) { |
239 | sco_disconnect_pcb(pcb, 0); |
240 | pcb->sp_link = NULL; |
241 | } |
242 | |
243 | LIST_REMOVE(pcb, sp_next); |
244 | free(pcb, M_BLUETOOTH); |
245 | } |
246 | |
247 | /* |
248 | * sco_listen_pcb(pcb) |
249 | * |
250 | * Mark pcb as a listener. |
251 | */ |
252 | int |
253 | sco_listen_pcb(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(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_pcb(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, sopt) |
315 | * |
316 | * Set SCO pcb options |
317 | */ |
318 | int |
319 | sco_setopt(struct sco_pcb *pcb, const struct sockopt *sopt) |
320 | { |
321 | int err = 0; |
322 | |
323 | switch (sopt->sopt_name) { |
324 | default: |
325 | err = ENOPROTOOPT; |
326 | break; |
327 | } |
328 | |
329 | return err; |
330 | } |
331 | |
332 | /* |
333 | * sco_getopt(pcb, sopt) |
334 | * |
335 | * Get SCO pcb options |
336 | */ |
337 | int |
338 | sco_getopt(struct sco_pcb *pcb, struct sockopt *sopt) |
339 | { |
340 | |
341 | switch (sopt->sopt_name) { |
342 | case SO_SCO_MTU: |
343 | return sockopt_set(sopt, &pcb->sp_mtu, sizeof(uint16_t)); |
344 | |
345 | case SO_SCO_HANDLE: |
346 | if (pcb->sp_link) |
347 | return sockopt_set(sopt, |
348 | &pcb->sp_link->hl_handle, sizeof(uint16_t)); |
349 | |
350 | return ENOTCONN; |
351 | |
352 | default: |
353 | break; |
354 | } |
355 | |
356 | return ENOPROTOOPT; |
357 | } |
358 | |