27 #include <linux/module.h>
35 static bool disable_esco;
37 static const struct proto_ops sco_sock_ops;
43 static void __sco_chan_add(
struct sco_conn *conn,
struct sock *
sk,
struct sock *parent);
44 static void sco_chan_del(
struct sock *
sk,
int err);
46 static void sco_sock_close(
struct sock *
sk);
47 static void sco_sock_kill(
struct sock *
sk);
50 static void sco_sock_timeout(
unsigned long arg)
54 BT_DBG(
"sock %p state %d", sk, sk->sk_state);
65 static void sco_sock_set_timer(
struct sock *sk,
long timeout)
67 BT_DBG(
"sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
71 static void sco_sock_clear_timer(
struct sock *sk)
73 BT_DBG(
"sock %p state %d", sk, sk->sk_state);
103 BT_DBG(
"hcon %p conn %p", hcon, conn);
117 static int sco_conn_del(
struct hci_conn *hcon,
int err)
125 BT_DBG(
"hcon %p conn %p, err %d", hcon, conn, err);
128 sk = sco_chan_get(conn);
131 sco_sock_clear_timer(sk);
132 sco_chan_del(sk, err);
141 hci_conn_put(conn->
hcon);
151 static int sco_chan_add(
struct sco_conn *conn,
struct sock *sk,
160 __sco_chan_add(conn, sk, parent);
166 static int sco_connect(
struct sock *sk)
195 conn = sco_conn_add(hcon);
203 bacpy(src, conn->
src);
205 err = sco_chan_add(conn, sk,
NULL);
210 sco_sock_clear_timer(sk);
223 static int sco_send_frame(
struct sock *sk,
struct msghdr *
msg,
int len)
233 BT_DBG(
"sk %p len %d", sk, len);
251 struct sock *sk = sco_chan_get(conn);
269 static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba)
278 if (!bacmp(&
bt_sk(sk)->src, ba))
288 static struct sock *sco_get_sock_listen(bdaddr_t *src)
300 if (!bacmp(&
bt_sk(sk)->src, src))
310 return node ? sk : sk1;
313 static void sco_sock_destruct(
struct sock *sk)
321 static void sco_sock_cleanup_listen(
struct sock *parent)
325 BT_DBG(
"parent %p", parent);
340 static void sco_sock_kill(
struct sock *sk)
345 BT_DBG(
"sk %p state %d", sk, sk->sk_state);
353 static void __sco_sock_close(
struct sock *sk)
357 switch (sk->sk_state) {
359 sco_sock_cleanup_listen(sk);
385 static void sco_sock_close(
struct sock *sk)
387 sco_sock_clear_timer(sk);
389 __sco_sock_close(sk);
394 static void sco_sock_init(
struct sock *sk,
struct sock *parent)
400 security_sk_clone(parent, sk);
404 static struct proto sco_proto = {
419 INIT_LIST_HEAD(&
bt_sk(sk)->accept_q);
435 static int sco_sock_create(
struct net *net,
struct socket *sock,
int protocol,
447 sock->
ops = &sco_sock_ops;
449 sk = sco_sock_alloc(net, sock, protocol,
GFP_ATOMIC);
453 sco_sock_init(sk,
NULL);
460 struct sock *sk = sock->
sk;
489 static int sco_sock_connect(
struct socket *sock,
struct sockaddr *addr,
int alen,
int flags)
492 struct sock *sk = sock->
sk;
513 err = sco_connect(sk);
527 struct sock *sk = sock->
sk;
528 bdaddr_t *src = &
bt_sk(sk)->src;
531 BT_DBG(
"sk %p backlog %d", sk, backlog);
547 if (__sco_get_sock_listen_by_addr(src)) {
565 static int sco_sock_accept(
struct socket *sock,
struct socket *newsock,
int flags)
568 struct sock *sk = sock->
sk, *ch;
574 timeo = sock_rcvtimeo(sk, flags &
O_NONBLOCK);
576 BT_DBG(
"sk %p timeo %ld", sk, timeo);
598 err = sock_intr_errno(timeo);
614 BT_DBG(
"new socket %p", ch);
621 static int sco_sock_getname(
struct socket *sock,
struct sockaddr *addr,
int *len,
int peer)
624 struct sock *sk = sock->
sk;
626 BT_DBG(
"sock %p, sk %p", sock, sk);
640 struct msghdr *msg,
size_t len)
642 struct sock *sk = sock->
sk;
645 BT_DBG(
"sock %p, sk %p", sock, sk);
647 err = sock_error(sk);
657 err = sco_send_frame(sk, msg, len);
665 static int sco_sock_setsockopt(
struct socket *sock,
int level,
int optname,
char __user *optval,
unsigned int optlen)
667 struct sock *sk = sock->
sk;
684 static int sco_sock_getsockopt_old(
struct socket *sock,
int optname,
char __user *optval,
int __user *optlen)
686 struct sock *sk = sock->
sk;
709 len =
min_t(
unsigned int, len,
sizeof(
opts));
721 memset(&cinfo, 0,
sizeof(cinfo));
722 cinfo.hci_handle =
sco_pi(sk)->conn->hcon->handle;
725 len =
min_t(
unsigned int, len,
sizeof(cinfo));
740 static int sco_sock_getsockopt(
struct socket *sock,
int level,
int optname,
char __user *optval,
int __user *optlen)
742 struct sock *sk = sock->
sk;
748 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
765 static int sco_sock_shutdown(
struct socket *sock,
int how)
767 struct sock *sk = sock->
sk;
770 BT_DBG(
"sock %p, sk %p", sock, sk);
778 sco_sock_clear_timer(sk);
779 __sco_sock_close(sk);
789 static int sco_sock_release(
struct socket *sock)
791 struct sock *sk = sock->
sk;
794 BT_DBG(
"sock %p, sk %p", sock, sk);
812 static void __sco_chan_add(
struct sco_conn *conn,
struct sock *sk,
struct sock *parent)
825 static void sco_chan_del(
struct sock *sk,
int err)
831 BT_DBG(
"sk %p, conn %p, err %d", sk, conn, err);
840 static void sco_conn_ready(
struct sco_conn *conn)
843 struct sock *sk = conn->
sk;
850 sco_sock_clear_timer(sk);
856 parent = sco_get_sock_listen(conn->
src);
862 sk = sco_sock_alloc(sock_net(parent),
NULL,
869 sco_sock_init(sk, parent);
874 hci_conn_hold(conn->
hcon);
875 __sco_chan_add(conn, sk, parent);
921 conn = sco_conn_add(hcon);
923 sco_conn_ready(conn);
930 BT_DBG(
"hcon %p reason %d", hcon, reason);
942 BT_DBG(
"conn %p len %d", conn, skb->
len);
945 sco_recv_frame(conn, skb);
954 static int sco_debugfs_show(
struct seq_file *
f,
void *
p)
977 .open = sco_debugfs_open,
983 static struct dentry *sco_debugfs;
985 static const struct proto_ops sco_sock_ops = {
988 .release = sco_sock_release,
989 .bind = sco_sock_bind,
990 .connect = sco_sock_connect,
991 .listen = sco_sock_listen,
992 .accept = sco_sock_accept,
993 .getname = sco_sock_getname,
994 .sendmsg = sco_sock_sendmsg,
1000 .shutdown = sco_sock_shutdown,
1001 .setsockopt = sco_sock_setsockopt,
1002 .getsockopt = sco_sock_getsockopt
1008 .create = sco_sock_create,
1021 BT_ERR(
"SCO socket registration failed");
1027 BT_ERR(
"Failed to create SCO proc file");
1034 NULL, &sco_debugfs_fops);
1036 BT_ERR(
"Failed to create SCO debug file");
1039 BT_INFO(
"SCO socket layer initialized");
1055 BT_ERR(
"SCO socket unregistration failed");