Home | History | Annotate | Line # | Download | only in netbt
sco_socket.c revision 1.36
      1 /*	$NetBSD: sco_socket.c,v 1.36 2015/04/26 21:40:49 rtr Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 Itronix Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of Itronix Inc. may not be used to endorse
     16  *    or promote products derived from this software without specific
     17  *    prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     26  * ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: sco_socket.c,v 1.36 2015/04/26 21:40:49 rtr Exp $");
     34 
     35 /* load symbolic names */
     36 #ifdef BLUETOOTH_DEBUG
     37 #define PRUREQUESTS
     38 #define PRCOREQUESTS
     39 #endif
     40 
     41 #include <sys/param.h>
     42 #include <sys/domain.h>
     43 #include <sys/kernel.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/proc.h>
     46 #include <sys/protosw.h>
     47 #include <sys/socket.h>
     48 #include <sys/socketvar.h>
     49 #include <sys/systm.h>
     50 
     51 #include <netbt/bluetooth.h>
     52 #include <netbt/hci.h>
     53 #include <netbt/sco.h>
     54 
     55 /*******************************************************************************
     56  *
     57  * SCO SOCK_SEQPACKET sockets - low latency audio data
     58  */
     59 
     60 static void sco_connecting(void *);
     61 static void sco_connected(void *);
     62 static void sco_disconnected(void *, int);
     63 static void *sco_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
     64 static void sco_complete(void *, int);
     65 static void sco_linkmode(void *, int);
     66 static void sco_input(void *, struct mbuf *);
     67 
     68 static const struct btproto sco_proto = {
     69 	sco_connecting,
     70 	sco_connected,
     71 	sco_disconnected,
     72 	sco_newconn,
     73 	sco_complete,
     74 	sco_linkmode,
     75 	sco_input,
     76 };
     77 
     78 int sco_sendspace = 4096;
     79 int sco_recvspace = 4096;
     80 
     81 static int
     82 sco_attach(struct socket *so, int proto)
     83 {
     84 	int error;
     85 
     86 	KASSERT(so->so_pcb == NULL);
     87 
     88 	if (so->so_lock == NULL) {
     89 		mutex_obj_hold(bt_lock);
     90 		so->so_lock = bt_lock;
     91 		solock(so);
     92 	}
     93 	KASSERT(solocked(so));
     94 
     95 	error = soreserve(so, sco_sendspace, sco_recvspace);
     96 	if (error) {
     97 		return error;
     98 	}
     99 	return sco_attach_pcb((struct sco_pcb **)&so->so_pcb, &sco_proto, so);
    100 }
    101 
    102 static void
    103 sco_detach(struct socket *so)
    104 {
    105 	KASSERT(so->so_pcb != NULL);
    106 	sco_detach_pcb((struct sco_pcb **)&so->so_pcb);
    107 	KASSERT(so->so_pcb == NULL);
    108 }
    109 
    110 static int
    111 sco_accept(struct socket *so, struct sockaddr *nam)
    112 {
    113 	struct sco_pcb *pcb = so->so_pcb;
    114 
    115 	KASSERT(solocked(so));
    116 	KASSERT(nam != NULL);
    117 
    118 	if (pcb == NULL)
    119 		return EINVAL;
    120 
    121 	return sco_peeraddr_pcb(pcb, (struct sockaddr_bt *)nam);
    122 }
    123 
    124 static int
    125 sco_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
    126 {
    127 	struct sco_pcb *pcb = so->so_pcb;
    128 	struct sockaddr_bt *sa = (struct sockaddr_bt *)nam;
    129 
    130 	KASSERT(solocked(so));
    131 	KASSERT(nam != NULL);
    132 
    133 	if (pcb == NULL)
    134 		return EINVAL;
    135 
    136 	if (sa->bt_len != sizeof(struct sockaddr_bt))
    137 		return EINVAL;
    138 
    139 	if (sa->bt_family != AF_BLUETOOTH)
    140 		return EAFNOSUPPORT;
    141 
    142 	return sco_bind_pcb(pcb, sa);
    143 }
    144 
    145 static int
    146 sco_listen(struct socket *so, struct lwp *l)
    147 {
    148 	struct sco_pcb *pcb = so->so_pcb;
    149 
    150 	KASSERT(solocked(so));
    151 
    152 	if (pcb == NULL)
    153 		return EINVAL;
    154 
    155 	return sco_listen_pcb(pcb);
    156 }
    157 
    158 static int
    159 sco_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
    160 {
    161 	struct sco_pcb *pcb = so->so_pcb;
    162 	struct sockaddr_bt *sa;
    163 
    164 	KASSERT(solocked(so));
    165 	KASSERT(nam != NULL);
    166 
    167 	if (pcb == NULL)
    168 		return EINVAL;
    169 
    170 	sa = mtod(nam, struct sockaddr_bt *);
    171 	if (sa->bt_len != sizeof(struct sockaddr_bt))
    172 		return EINVAL;
    173 
    174 	if (sa->bt_family != AF_BLUETOOTH)
    175 		return EAFNOSUPPORT;
    176 
    177 	soisconnecting(so);
    178 	return sco_connect_pcb(pcb, sa);
    179 }
    180 
    181 static int
    182 sco_connect2(struct socket *so, struct socket *so2)
    183 {
    184 	struct sco_pcb *pcb = so->so_pcb;
    185 
    186 	KASSERT(solocked(so));
    187 
    188 	if (pcb == NULL)
    189 		return EINVAL;
    190 
    191 	return EOPNOTSUPP;
    192 }
    193 
    194 static int
    195 sco_disconnect(struct socket *so)
    196 {
    197 	struct sco_pcb *pcb = so->so_pcb;
    198 
    199 	KASSERT(solocked(so));
    200 
    201 	if (pcb == NULL)
    202 		return EINVAL;
    203 
    204 	soisdisconnecting(so);
    205 	return sco_disconnect_pcb(pcb, so->so_linger);
    206 }
    207 
    208 static int
    209 sco_shutdown(struct socket *so)
    210 {
    211 	KASSERT(solocked(so));
    212 
    213 	socantsendmore(so);
    214 	return 0;
    215 }
    216 
    217 static int
    218 sco_abort(struct socket *so)
    219 {
    220 	struct sco_pcb *pcb = so->so_pcb;
    221 
    222 	KASSERT(solocked(so));
    223 
    224 	if (pcb == NULL)
    225 		return EINVAL;
    226 
    227 	sco_disconnect_pcb(pcb, 0);
    228 	soisdisconnected(so);
    229 	sco_detach(so);
    230 	return 0;
    231 }
    232 
    233 static int
    234 sco_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
    235 {
    236 	return EOPNOTSUPP;
    237 }
    238 
    239 static int
    240 sco_stat(struct socket *so, struct stat *ub)
    241 {
    242 	KASSERT(solocked(so));
    243 
    244 	return 0;
    245 }
    246 
    247 static int
    248 sco_peeraddr(struct socket *so, struct sockaddr *nam)
    249 {
    250 	struct sco_pcb *pcb = (struct sco_pcb *)so->so_pcb;
    251 
    252 	KASSERT(solocked(so));
    253 	KASSERT(pcb != NULL);
    254 	KASSERT(nam != NULL);
    255 
    256 	return sco_peeraddr_pcb(pcb, (struct sockaddr_bt *)nam);
    257 }
    258 
    259 static int
    260 sco_sockaddr(struct socket *so, struct sockaddr *nam)
    261 {
    262 	struct sco_pcb *pcb = (struct sco_pcb *)so->so_pcb;
    263 
    264 	KASSERT(solocked(so));
    265 	KASSERT(pcb != NULL);
    266 	KASSERT(nam != NULL);
    267 
    268 	return sco_sockaddr_pcb(pcb, (struct sockaddr_bt *)nam);
    269 }
    270 
    271 static int
    272 sco_rcvd(struct socket *so, int flags, struct lwp *l)
    273 {
    274 	KASSERT(solocked(so));
    275 
    276 	return EOPNOTSUPP;
    277 }
    278 
    279 static int
    280 sco_recvoob(struct socket *so, struct mbuf *m, int flags)
    281 {
    282 	KASSERT(solocked(so));
    283 
    284 	return EOPNOTSUPP;
    285 }
    286 
    287 static int
    288 sco_send(struct socket *so, struct mbuf *m, struct mbuf *nam,
    289     struct mbuf *control, struct lwp *l)
    290 {
    291 	struct sco_pcb *pcb = so->so_pcb;
    292 	int err = 0;
    293 	struct mbuf *m0;
    294 
    295 	KASSERT(solocked(so));
    296 	KASSERT(m != NULL);
    297 
    298 	if (control) /* no use for that */
    299 		m_freem(control);
    300 
    301 	if (pcb == NULL) {
    302 		err = EINVAL;
    303 		goto release;
    304 	}
    305 
    306 	if (m->m_pkthdr.len == 0)
    307 		goto release;
    308 
    309 	if (m->m_pkthdr.len > pcb->sp_mtu) {
    310 		err = EMSGSIZE;
    311 		goto release;
    312 	}
    313 
    314 	m0 = m_copypacket(m, M_DONTWAIT);
    315 	if (m0 == NULL) {
    316 		err = ENOMEM;
    317 		goto release;
    318 	}
    319 
    320 	sbappendrecord(&so->so_snd, m);
    321 	return sco_send_pcb(pcb, m0);
    322 
    323 release:
    324 	m_freem(m);
    325 	return err;
    326 }
    327 
    328 static int
    329 sco_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
    330 {
    331 	KASSERT(solocked(so));
    332 
    333 	if (m)
    334 		m_freem(m);
    335 	if (control)
    336 		m_freem(control);
    337 
    338 	return EOPNOTSUPP;
    339 }
    340 
    341 static int
    342 sco_purgeif(struct socket *so, struct ifnet *ifp)
    343 {
    344 
    345 	return EOPNOTSUPP;
    346 }
    347 
    348 /*
    349  * get/set socket options
    350  */
    351 int
    352 sco_ctloutput(int req, struct socket *so, struct sockopt *sopt)
    353 {
    354 	struct sco_pcb *pcb = (struct sco_pcb *)so->so_pcb;
    355 	int err = 0;
    356 
    357 	DPRINTFN(2, "req %s\n", prcorequests[req]);
    358 
    359 	if (pcb == NULL)
    360 		return EINVAL;
    361 
    362 	if (sopt->sopt_level != BTPROTO_SCO)
    363 		return ENOPROTOOPT;
    364 
    365 	switch(req) {
    366 	case PRCO_GETOPT:
    367 		err = sco_getopt(pcb, sopt);
    368 		break;
    369 
    370 	case PRCO_SETOPT:
    371 		err = sco_setopt(pcb, sopt);
    372 		break;
    373 
    374 	default:
    375 		err = ENOPROTOOPT;
    376 		break;
    377 	}
    378 
    379 	return err;
    380 }
    381 
    382 /*****************************************************************************
    383  *
    384  *	SCO Protocol socket callbacks
    385  *
    386  */
    387 static void
    388 sco_connecting(void *arg)
    389 {
    390 	struct socket *so = arg;
    391 
    392 	DPRINTF("Connecting\n");
    393 	soisconnecting(so);
    394 }
    395 
    396 static void
    397 sco_connected(void *arg)
    398 {
    399 	struct socket *so = arg;
    400 
    401 	DPRINTF("Connected\n");
    402 	soisconnected(so);
    403 }
    404 
    405 static void
    406 sco_disconnected(void *arg, int err)
    407 {
    408 	struct socket *so = arg;
    409 
    410 	DPRINTF("Disconnected (%d)\n", err);
    411 
    412 	so->so_error = err;
    413 	soisdisconnected(so);
    414 }
    415 
    416 static void *
    417 sco_newconn(void *arg, struct sockaddr_bt *laddr,
    418     struct sockaddr_bt *raddr)
    419 {
    420 	struct socket *so = arg;
    421 
    422 	DPRINTF("New Connection\n");
    423 	so = sonewconn(so, false);
    424 	if (so == NULL)
    425 		return NULL;
    426 
    427 	soisconnecting(so);
    428 	return so->so_pcb;
    429 }
    430 
    431 static void
    432 sco_complete(void *arg, int num)
    433 {
    434 	struct socket *so = arg;
    435 
    436 	while (num-- > 0)
    437 		sbdroprecord(&so->so_snd);
    438 
    439 	sowwakeup(so);
    440 }
    441 
    442 static void
    443 sco_linkmode(void *arg, int mode)
    444 {
    445 }
    446 
    447 static void
    448 sco_input(void *arg, struct mbuf *m)
    449 {
    450 	struct socket *so = arg;
    451 
    452 	/*
    453 	 * since this data is time sensitive, if the buffer
    454 	 * is full we just dump data until the latest one
    455 	 * will fit.
    456 	 */
    457 
    458 	while (m->m_pkthdr.len > sbspace(&so->so_rcv))
    459 		sbdroprecord(&so->so_rcv);
    460 
    461 	DPRINTFN(10, "received %d bytes\n", m->m_pkthdr.len);
    462 
    463 	sbappendrecord(&so->so_rcv, m);
    464 	sorwakeup(so);
    465 }
    466 
    467 PR_WRAP_USRREQS(sco)
    468 
    469 #define	sco_attach		sco_attach_wrapper
    470 #define	sco_detach		sco_detach_wrapper
    471 #define	sco_accept		sco_accept_wrapper
    472 #define	sco_bind		sco_bind_wrapper
    473 #define	sco_listen		sco_listen_wrapper
    474 #define	sco_connect		sco_connect_wrapper
    475 #define	sco_connect2		sco_connect2_wrapper
    476 #define	sco_disconnect		sco_disconnect_wrapper
    477 #define	sco_shutdown		sco_shutdown_wrapper
    478 #define	sco_abort		sco_abort_wrapper
    479 #define	sco_ioctl		sco_ioctl_wrapper
    480 #define	sco_stat		sco_stat_wrapper
    481 #define	sco_peeraddr		sco_peeraddr_wrapper
    482 #define	sco_sockaddr		sco_sockaddr_wrapper
    483 #define	sco_rcvd		sco_rcvd_wrapper
    484 #define	sco_recvoob		sco_recvoob_wrapper
    485 #define	sco_send		sco_send_wrapper
    486 #define	sco_sendoob		sco_sendoob_wrapper
    487 #define	sco_purgeif		sco_purgeif_wrapper
    488 
    489 const struct pr_usrreqs sco_usrreqs = {
    490 	.pr_attach	= sco_attach,
    491 	.pr_detach	= sco_detach,
    492 	.pr_accept	= sco_accept,
    493 	.pr_bind	= sco_bind,
    494 	.pr_listen	= sco_listen,
    495 	.pr_connect	= sco_connect,
    496 	.pr_connect2	= sco_connect2,
    497 	.pr_disconnect	= sco_disconnect,
    498 	.pr_shutdown	= sco_shutdown,
    499 	.pr_abort	= sco_abort,
    500 	.pr_ioctl	= sco_ioctl,
    501 	.pr_stat	= sco_stat,
    502 	.pr_peeraddr	= sco_peeraddr,
    503 	.pr_sockaddr	= sco_sockaddr,
    504 	.pr_rcvd	= sco_rcvd,
    505 	.pr_recvoob	= sco_recvoob,
    506 	.pr_send	= sco_send,
    507 	.pr_sendoob	= sco_sendoob,
    508 	.pr_purgeif	= sco_purgeif,
    509 };
    510