1 /* $OpenBSD: sco_socket.c,v 1.1 2007/06/01 02:46:12 uwe Exp $ */
2 /* $NetBSD: sco_socket.c,v 1.9 2007/04/21 06:15:23 plunky Exp $ */
5 * Copyright (c) 2006 Itronix Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of Itronix Inc. may not be used to endorse
17 * or promote products derived from this software without specific
18 * prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 /* load symbolic names */
34 #ifdef BLUETOOTH_DEBUG
39 #include <sys/param.h>
40 #include <sys/domain.h>
41 #include <sys/kernel.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/systm.h>
50 #include <netbt/bluetooth.h>
51 #include <netbt/hci.h>
52 #include <netbt/sco.h>
54 /*******************************************************************************
56 * SCO SOCK_SEQPACKET sockets - low latency audio data
59 static void sco_connecting(void *);
60 static void sco_connected(void *);
61 static void sco_disconnected(void *, int);
62 static void *sco_newconn(void *, struct sockaddr_bt
*, struct sockaddr_bt
*);
63 static void sco_complete(void *, int);
64 static void sco_linkmode(void *, int);
65 static void sco_input(void *, struct mbuf
*);
67 static const struct btproto sco_proto
= {
77 int sco_sendspace
= 4096;
78 int sco_recvspace
= 4096;
81 * get/set socket options
84 sco_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
86 struct sco_pcb
*pcb
= (struct sco_pcb
*)so
->so_pcb
;
90 #ifdef notyet /* XXX */
91 DPRINTFN(2, "req %s\n", prcorequests
[req
]);
97 if (sopt
->sopt_level
!= BTPROTO_SCO
)
100 switch(sopt
->sopt_dir
) {
102 m
= m_get(MB_WAIT
, MT_DATA
);
103 m
->m_len
= sco_getopt(pcb
, sopt
->sopt_name
, mtod(m
, uint8_t *));
110 /* XXX There are possible memory leaks (Griffin) */
111 soopt_from_kbuf(sopt
, mtod(m
, void *), m
->m_len
);
115 m
= m_get(M_WAITOK
, MT_DATA
);
117 err
= soopt_to_kbuf(sopt
, mtod(m
,void*), m
->m_len
, m
->m_len
);
125 err
= sco_setopt(pcb
, sopt
->sopt_name
, mtod(m
, uint8_t *));
137 /*****************************************************************************
139 * SCO Protocol socket callbacks
143 sco_connecting(void *arg
)
145 struct socket
*so
= arg
;
147 DPRINTF("Connecting\n");
152 sco_connected(void *arg
)
154 struct socket
*so
= arg
;
156 DPRINTF("Connected\n");
161 sco_disconnected(void *arg
, int err
)
163 struct socket
*so
= arg
;
165 DPRINTF("Disconnected (%d)\n", err
);
168 soisdisconnected(so
);
172 sco_newconn(void *arg
, struct sockaddr_bt
*laddr
,
173 struct sockaddr_bt
*raddr
)
175 struct socket
*so
= arg
;
177 DPRINTF("New Connection\n");
178 so
= sonewconn(so
, 0);
187 sco_complete(void *arg
, int num
)
189 struct socket
*so
= arg
;
192 sbdroprecord(&so
->so_snd
.sb
);
198 sco_linkmode(void *arg
, int mode
)
203 sco_input(void *arg
, struct mbuf
*m
)
205 struct socket
*so
= arg
;
208 * since this data is time sensitive, if the buffer
209 * is full we just dump data until the latest one
213 while (m
->m_pkthdr
.len
> sbspace(&so
->so_rcv
))
214 sbdroprecord(&so
->so_rcv
.sb
);
216 DPRINTFN(10, "received %d bytes\n", m
->m_pkthdr
.len
);
218 sbappendrecord(&so
->so_rcv
.sb
, m
);
223 * Implementation of usrreqs.
226 sco_sdetach(struct socket
*so
)
228 return sco_detach((struct sco_pcb
**)&so
->so_pcb
);
232 sco_sabort (struct socket
*so
)
234 struct sco_pcb
*pcb
= (struct sco_pcb
*)so
->so_pcb
;
236 sco_disconnect(pcb
, 0);
237 soisdisconnected(so
);
239 return sco_sdetach(so
);
243 sco_sdisconnect (struct socket
*so
)
245 struct sco_pcb
*pcb
= (struct sco_pcb
*)so
->so_pcb
;
247 soisdisconnecting(so
);
249 return sco_disconnect(pcb
, so
->so_linger
);
253 sco_sattach (struct socket
*so
, int proto
,
254 struct pru_attach_info
*ai
)
256 struct sco_pcb
*pcb
= (struct sco_pcb
*)so
->so_pcb
;
262 err
= soreserve(so
, sco_sendspace
, sco_recvspace
,NULL
);
266 return sco_attach((struct sco_pcb
**)&so
->so_pcb
,
271 sco_sbind (struct socket
*so
, struct sockaddr
*nam
,
274 struct sco_pcb
*pcb
= (struct sco_pcb
*)so
->so_pcb
;
275 struct sockaddr_bt
*sa
;
277 KKASSERT(nam
!= NULL
);
278 sa
= (struct sockaddr_bt
*)nam
;
280 if (sa
->bt_len
!= sizeof(struct sockaddr_bt
))
283 if (sa
->bt_family
!= AF_BLUETOOTH
)
286 return sco_bind(pcb
, sa
);
290 sco_sconnect (struct socket
*so
, struct sockaddr
*nam
,
293 struct sco_pcb
*pcb
= (struct sco_pcb
*)so
->so_pcb
;
294 struct sockaddr_bt
*sa
;
296 KKASSERT(nam
!= NULL
);
297 sa
= (struct sockaddr_bt
*)nam
;
299 if (sa
->bt_len
!= sizeof(struct sockaddr_bt
))
302 if (sa
->bt_family
!= AF_BLUETOOTH
)
306 return sco_connect(pcb
, sa
);
310 sco_speeraddr (struct socket
*so
, struct sockaddr
**nam
)
312 struct sco_pcb
*pcb
= (struct sco_pcb
*)so
->so_pcb
;
313 struct sockaddr_bt
*sa
, ssa
;
317 bzero(sa
, sizeof *sa
);
318 sa
->bt_len
= sizeof(struct sockaddr_bt
);
319 sa
->bt_family
= AF_BLUETOOTH
;
320 e
= sco_peeraddr(pcb
, sa
);
321 *nam
= dup_sockaddr((struct sockaddr
*)sa
);
327 sco_ssockaddr (struct socket
*so
, struct sockaddr
**nam
)
329 struct sco_pcb
*pcb
= (struct sco_pcb
*)so
->so_pcb
;
330 struct sockaddr_bt
*sa
, ssa
;
334 bzero(sa
, sizeof *sa
);
335 sa
->bt_len
= sizeof(struct sockaddr_bt
);
336 sa
->bt_family
= AF_BLUETOOTH
;
337 e
= sco_sockaddr(pcb
, sa
);
338 *nam
= dup_sockaddr((struct sockaddr
*)sa
);
344 sco_sshutdown (struct socket
*so
)
351 sco_ssend (struct socket
*so
, int flags
, struct mbuf
*m
,
352 struct sockaddr
*addr
, struct mbuf
*control
, struct thread
*td
)
354 struct sco_pcb
*pcb
= (struct sco_pcb
*)so
->so_pcb
;
359 if (m
->m_pkthdr
.len
== 0)
362 if (m
->m_pkthdr
.len
> pcb
->sp_mtu
) {
367 m0
= m_copym(m
, 0, M_COPYALL
, MB_DONTWAIT
);
373 if (control
) /* no use for that */
376 sbappendrecord(&so
->so_snd
.sb
, m
);
377 return sco_send(pcb
, m0
);
381 if (control
) m_freem(control
);
386 sco_saccept(struct socket
*so
, struct sockaddr
**nam
)
388 struct sco_pcb
*pcb
= (struct sco_pcb
*)so
->so_pcb
;
389 struct sockaddr_bt
*sa
, ssa
;
393 bzero(sa
, sizeof *sa
);
394 sa
->bt_len
= sizeof(struct sockaddr_bt
);
395 sa
->bt_family
= AF_BLUETOOTH
;
396 e
= sco_peeraddr(pcb
, sa
);
397 *nam
= dup_sockaddr((struct sockaddr
*)sa
);
403 sco_slisten(struct socket
*so
, struct thread
*td
)
405 struct sco_pcb
*pcb
= (struct sco_pcb
*)so
->so_pcb
;
406 return sco_listen(pcb
);
410 struct pr_usrreqs sco_usrreqs
= {
411 .pru_abort
= sco_sabort
,
412 .pru_accept
= sco_saccept
,
413 .pru_attach
= sco_sattach
,
414 .pru_bind
= sco_sbind
,
415 .pru_connect
= sco_sconnect
,
416 .pru_connect2
= pru_connect2_notsupp
,
417 .pru_control
= pru_control_notsupp
,
418 .pru_detach
= sco_sdetach
,
419 .pru_disconnect
= sco_sdisconnect
,
420 .pru_listen
= sco_slisten
,
421 .pru_peeraddr
= sco_speeraddr
,
422 .pru_rcvd
= pru_rcvd_notsupp
,
423 .pru_rcvoob
= pru_rcvoob_notsupp
,
424 .pru_send
= sco_ssend
,
425 .pru_sense
= pru_sense_null
,
426 .pru_shutdown
= sco_sshutdown
,
427 .pru_sockaddr
= sco_ssockaddr
,
428 .pru_sosend
= sosend
,
429 .pru_soreceive
= soreceive
,