drm/vc4: Add gem_info node via debugfs for vc5
[platform/kernel/linux-rpi.git] / net / bluetooth / sco.c
1 /*
2    BlueZ - Bluetooth protocol stack for Linux
3    Copyright (C) 2000-2001 Qualcomm Incorporated
4
5    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License version 2 as
9    published by the Free Software Foundation;
10
11    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22    SOFTWARE IS DISCLAIMED.
23 */
24
25 /* Bluetooth SCO sockets. */
26
27 #include <linux/module.h>
28 #include <linux/debugfs.h>
29 #include <linux/seq_file.h>
30 #include <linux/sched/signal.h>
31
32 #include <net/bluetooth/bluetooth.h>
33 #include <net/bluetooth/hci_core.h>
34 #include <net/bluetooth/sco.h>
35
36 #ifdef TIZEN_BT
37 #include <net/bluetooth/mgmt.h>
38 #include <net/bluetooth/mgmt_tizen.h>
39 #endif
40
41 static bool disable_esco;
42
43 static const struct proto_ops sco_sock_ops;
44
45 static struct bt_sock_list sco_sk_list = {
46         .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock)
47 };
48
49 /* ---- SCO connections ---- */
50 struct sco_conn {
51         struct hci_conn *hcon;
52
53         spinlock_t      lock;
54         struct sock     *sk;
55
56         struct delayed_work     timeout_work;
57
58         unsigned int    mtu;
59 };
60
61 #define sco_conn_lock(c)        spin_lock(&c->lock)
62 #define sco_conn_unlock(c)      spin_unlock(&c->lock)
63
64 static void sco_sock_close(struct sock *sk);
65 static void sco_sock_kill(struct sock *sk);
66
67 /* ----- SCO socket info ----- */
68 #define sco_pi(sk) ((struct sco_pinfo *) sk)
69
70 struct sco_pinfo {
71         struct bt_sock  bt;
72         bdaddr_t        src;
73         bdaddr_t        dst;
74         __u32           flags;
75         __u16           setting;
76         __u8            cmsg_mask;
77         struct sco_conn *conn;
78 };
79
80 /* ---- SCO timers ---- */
81 #define SCO_CONN_TIMEOUT        (HZ * 40)
82 #define SCO_DISCONN_TIMEOUT     (HZ * 2)
83
84 static void sco_sock_timeout(struct work_struct *work)
85 {
86         struct sco_conn *conn = container_of(work, struct sco_conn,
87                                              timeout_work.work);
88         struct sock *sk;
89
90         sco_conn_lock(conn);
91         sk = conn->sk;
92         if (sk)
93                 sock_hold(sk);
94         sco_conn_unlock(conn);
95
96         if (!sk)
97                 return;
98
99         BT_DBG("sock %p state %d", sk, sk->sk_state);
100
101         lock_sock(sk);
102         sk->sk_err = ETIMEDOUT;
103         sk->sk_state_change(sk);
104         release_sock(sk);
105         sock_put(sk);
106 }
107
108 static void sco_sock_set_timer(struct sock *sk, long timeout)
109 {
110         if (!sco_pi(sk)->conn)
111                 return;
112
113         BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
114         cancel_delayed_work(&sco_pi(sk)->conn->timeout_work);
115         schedule_delayed_work(&sco_pi(sk)->conn->timeout_work, timeout);
116 }
117
118 static void sco_sock_clear_timer(struct sock *sk)
119 {
120         if (!sco_pi(sk)->conn)
121                 return;
122
123         BT_DBG("sock %p state %d", sk, sk->sk_state);
124         cancel_delayed_work(&sco_pi(sk)->conn->timeout_work);
125 }
126
127 /* ---- SCO connections ---- */
128 static struct sco_conn *sco_conn_add(struct hci_conn *hcon)
129 {
130         struct hci_dev *hdev = hcon->hdev;
131         struct sco_conn *conn = hcon->sco_data;
132
133         if (conn)
134                 return conn;
135
136         conn = kzalloc(sizeof(struct sco_conn), GFP_KERNEL);
137         if (!conn)
138                 return NULL;
139
140         spin_lock_init(&conn->lock);
141         INIT_DELAYED_WORK(&conn->timeout_work, sco_sock_timeout);
142
143         hcon->sco_data = conn;
144         conn->hcon = hcon;
145
146         if (hdev->sco_mtu > 0)
147                 conn->mtu = hdev->sco_mtu;
148         else
149                 conn->mtu = 60;
150
151         BT_DBG("hcon %p conn %p", hcon, conn);
152
153         return conn;
154 }
155
156 /* Delete channel.
157  * Must be called on the locked socket. */
158 static void sco_chan_del(struct sock *sk, int err)
159 {
160         struct sco_conn *conn;
161
162         conn = sco_pi(sk)->conn;
163
164         BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
165
166         if (conn) {
167                 sco_conn_lock(conn);
168                 conn->sk = NULL;
169                 sco_pi(sk)->conn = NULL;
170                 sco_conn_unlock(conn);
171
172                 if (conn->hcon)
173                         hci_conn_drop(conn->hcon);
174         }
175
176         sk->sk_state = BT_CLOSED;
177         sk->sk_err   = err;
178         sk->sk_state_change(sk);
179
180         sock_set_flag(sk, SOCK_ZAPPED);
181 }
182
183 static void sco_conn_del(struct hci_conn *hcon, int err)
184 {
185         struct sco_conn *conn = hcon->sco_data;
186         struct sock *sk;
187
188         if (!conn)
189                 return;
190
191         BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
192
193         /* Kill socket */
194         sco_conn_lock(conn);
195         sk = conn->sk;
196         if (sk)
197                 sock_hold(sk);
198         sco_conn_unlock(conn);
199
200         if (sk) {
201                 lock_sock(sk);
202                 sco_sock_clear_timer(sk);
203                 sco_chan_del(sk, err);
204                 release_sock(sk);
205                 sock_put(sk);
206         }
207
208         /* Ensure no more work items will run before freeing conn. */
209         cancel_delayed_work_sync(&conn->timeout_work);
210
211         hcon->sco_data = NULL;
212         kfree(conn);
213 }
214
215 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk,
216                            struct sock *parent)
217 {
218         BT_DBG("conn %p", conn);
219
220         sco_pi(sk)->conn = conn;
221         conn->sk = sk;
222
223         if (parent)
224                 bt_accept_enqueue(parent, sk, true);
225 }
226
227 static int sco_chan_add(struct sco_conn *conn, struct sock *sk,
228                         struct sock *parent)
229 {
230         int err = 0;
231
232         sco_conn_lock(conn);
233         if (conn->sk)
234                 err = -EBUSY;
235         else
236                 __sco_chan_add(conn, sk, parent);
237
238         sco_conn_unlock(conn);
239         return err;
240 }
241
242 static int sco_connect(struct hci_dev *hdev, struct sock *sk)
243 {
244         struct sco_conn *conn;
245         struct hci_conn *hcon;
246         int err, type;
247
248         BT_DBG("%pMR -> %pMR", &sco_pi(sk)->src, &sco_pi(sk)->dst);
249
250         if (lmp_esco_capable(hdev) && !disable_esco)
251                 type = ESCO_LINK;
252         else
253                 type = SCO_LINK;
254
255         if (sco_pi(sk)->setting == BT_VOICE_TRANSPARENT &&
256             (!lmp_transp_capable(hdev) || !lmp_esco_capable(hdev)))
257                 return -EOPNOTSUPP;
258
259         hcon = hci_connect_sco(hdev, type, &sco_pi(sk)->dst,
260                                sco_pi(sk)->setting);
261         if (IS_ERR(hcon))
262                 return PTR_ERR(hcon);
263
264         conn = sco_conn_add(hcon);
265         if (!conn) {
266                 hci_conn_drop(hcon);
267                 return -ENOMEM;
268         }
269
270         /* Update source addr of the socket */
271         bacpy(&sco_pi(sk)->src, &hcon->src);
272
273         err = sco_chan_add(conn, sk, NULL);
274         if (err)
275                 return err;
276
277         if (hcon->state == BT_CONNECTED) {
278                 sco_sock_clear_timer(sk);
279                 sk->sk_state = BT_CONNECTED;
280         } else {
281                 sk->sk_state = BT_CONNECT;
282                 sco_sock_set_timer(sk, sk->sk_sndtimeo);
283         }
284
285         return err;
286 }
287
288 static int sco_send_frame(struct sock *sk, struct sk_buff *skb)
289 {
290         struct sco_conn *conn = sco_pi(sk)->conn;
291         int len = skb->len;
292
293         /* Check outgoing MTU */
294         if (len > conn->mtu)
295                 return -EINVAL;
296
297         BT_DBG("sk %p len %d", sk, len);
298
299         hci_send_sco(conn->hcon, skb);
300
301         return len;
302 }
303
304 static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
305 {
306         struct sock *sk;
307
308         sco_conn_lock(conn);
309         sk = conn->sk;
310         sco_conn_unlock(conn);
311
312         if (!sk)
313                 goto drop;
314
315         BT_DBG("sk %p len %u", sk, skb->len);
316
317         if (sk->sk_state != BT_CONNECTED)
318                 goto drop;
319
320         if (!sock_queue_rcv_skb(sk, skb))
321                 return;
322
323 drop:
324         kfree_skb(skb);
325 }
326
327 /* -------- Socket interface ---------- */
328 static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba)
329 {
330         struct sock *sk;
331
332         sk_for_each(sk, &sco_sk_list.head) {
333                 if (sk->sk_state != BT_LISTEN)
334                         continue;
335
336                 if (!bacmp(&sco_pi(sk)->src, ba))
337                         return sk;
338         }
339
340         return NULL;
341 }
342
343 /* Find socket listening on source bdaddr.
344  * Returns closest match.
345  */
346 static struct sock *sco_get_sock_listen(bdaddr_t *src)
347 {
348         struct sock *sk = NULL, *sk1 = NULL;
349
350         read_lock(&sco_sk_list.lock);
351
352         sk_for_each(sk, &sco_sk_list.head) {
353                 if (sk->sk_state != BT_LISTEN)
354                         continue;
355
356                 /* Exact match. */
357                 if (!bacmp(&sco_pi(sk)->src, src))
358                         break;
359
360                 /* Closest match */
361                 if (!bacmp(&sco_pi(sk)->src, BDADDR_ANY))
362                         sk1 = sk;
363         }
364
365         read_unlock(&sco_sk_list.lock);
366
367         return sk ? sk : sk1;
368 }
369
370 static void sco_sock_destruct(struct sock *sk)
371 {
372         BT_DBG("sk %p", sk);
373
374         skb_queue_purge(&sk->sk_receive_queue);
375         skb_queue_purge(&sk->sk_write_queue);
376 }
377
378 static void sco_sock_cleanup_listen(struct sock *parent)
379 {
380         struct sock *sk;
381
382         BT_DBG("parent %p", parent);
383
384         /* Close not yet accepted channels */
385         while ((sk = bt_accept_dequeue(parent, NULL))) {
386                 sco_sock_close(sk);
387                 sco_sock_kill(sk);
388         }
389
390         parent->sk_state  = BT_CLOSED;
391         sock_set_flag(parent, SOCK_ZAPPED);
392 }
393
394 /* Kill socket (only if zapped and orphan)
395  * Must be called on unlocked socket.
396  */
397 static void sco_sock_kill(struct sock *sk)
398 {
399         if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
400                 return;
401
402         BT_DBG("sk %p state %d", sk, sk->sk_state);
403
404         /* Kill poor orphan */
405         bt_sock_unlink(&sco_sk_list, sk);
406         sock_set_flag(sk, SOCK_DEAD);
407         sock_put(sk);
408 }
409
410 static void __sco_sock_close(struct sock *sk)
411 {
412         BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
413
414         switch (sk->sk_state) {
415         case BT_LISTEN:
416                 sco_sock_cleanup_listen(sk);
417                 break;
418
419         case BT_CONNECTED:
420         case BT_CONFIG:
421                 if (sco_pi(sk)->conn->hcon) {
422                         sk->sk_state = BT_DISCONN;
423                         sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT);
424                         sco_conn_lock(sco_pi(sk)->conn);
425                         hci_conn_drop(sco_pi(sk)->conn->hcon);
426                         sco_pi(sk)->conn->hcon = NULL;
427                         sco_conn_unlock(sco_pi(sk)->conn);
428                 } else
429                         sco_chan_del(sk, ECONNRESET);
430                 break;
431
432         case BT_CONNECT2:
433         case BT_CONNECT:
434         case BT_DISCONN:
435                 sco_chan_del(sk, ECONNRESET);
436                 break;
437
438         default:
439                 sock_set_flag(sk, SOCK_ZAPPED);
440                 break;
441         }
442 }
443
444 /* Must be called on unlocked socket. */
445 static void sco_sock_close(struct sock *sk)
446 {
447         lock_sock(sk);
448         sco_sock_clear_timer(sk);
449         __sco_sock_close(sk);
450         release_sock(sk);
451 }
452
453 static void sco_skb_put_cmsg(struct sk_buff *skb, struct msghdr *msg,
454                              struct sock *sk)
455 {
456         if (sco_pi(sk)->cmsg_mask & SCO_CMSG_PKT_STATUS)
457                 put_cmsg(msg, SOL_BLUETOOTH, BT_SCM_PKT_STATUS,
458                          sizeof(bt_cb(skb)->sco.pkt_status),
459                          &bt_cb(skb)->sco.pkt_status);
460 }
461
462 static void sco_sock_init(struct sock *sk, struct sock *parent)
463 {
464         BT_DBG("sk %p", sk);
465
466         if (parent) {
467                 sk->sk_type = parent->sk_type;
468                 bt_sk(sk)->flags = bt_sk(parent)->flags;
469                 security_sk_clone(parent, sk);
470         } else {
471                 bt_sk(sk)->skb_put_cmsg = sco_skb_put_cmsg;
472         }
473 }
474
475 static struct proto sco_proto = {
476         .name           = "SCO",
477         .owner          = THIS_MODULE,
478         .obj_size       = sizeof(struct sco_pinfo)
479 };
480
481 static struct sock *sco_sock_alloc(struct net *net, struct socket *sock,
482                                    int proto, gfp_t prio, int kern)
483 {
484         struct sock *sk;
485
486         sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto, kern);
487         if (!sk)
488                 return NULL;
489
490         sock_init_data(sock, sk);
491         INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
492
493         sk->sk_destruct = sco_sock_destruct;
494         sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
495
496         sock_reset_flag(sk, SOCK_ZAPPED);
497
498         sk->sk_protocol = proto;
499         sk->sk_state    = BT_OPEN;
500
501         sco_pi(sk)->setting = BT_VOICE_CVSD_16BIT;
502
503         bt_sock_link(&sco_sk_list, sk);
504         return sk;
505 }
506
507 static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
508                            int kern)
509 {
510         struct sock *sk;
511
512         BT_DBG("sock %p", sock);
513
514         sock->state = SS_UNCONNECTED;
515
516         if (sock->type != SOCK_SEQPACKET)
517                 return -ESOCKTNOSUPPORT;
518
519         sock->ops = &sco_sock_ops;
520
521         sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
522         if (!sk)
523                 return -ENOMEM;
524
525         sco_sock_init(sk, NULL);
526         return 0;
527 }
528
529 static int sco_sock_bind(struct socket *sock, struct sockaddr *addr,
530                          int addr_len)
531 {
532         struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
533         struct sock *sk = sock->sk;
534         int err = 0;
535
536         if (!addr || addr_len < sizeof(struct sockaddr_sco) ||
537             addr->sa_family != AF_BLUETOOTH)
538                 return -EINVAL;
539
540         BT_DBG("sk %p %pMR", sk, &sa->sco_bdaddr);
541
542         lock_sock(sk);
543
544         if (sk->sk_state != BT_OPEN) {
545                 err = -EBADFD;
546                 goto done;
547         }
548
549         if (sk->sk_type != SOCK_SEQPACKET) {
550                 err = -EINVAL;
551                 goto done;
552         }
553
554         bacpy(&sco_pi(sk)->src, &sa->sco_bdaddr);
555
556         sk->sk_state = BT_BOUND;
557
558 done:
559         release_sock(sk);
560         return err;
561 }
562
563 static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
564 {
565         struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
566         struct sock *sk = sock->sk;
567         struct hci_dev  *hdev;
568         int err;
569
570         BT_DBG("sk %p", sk);
571
572         if (alen < sizeof(struct sockaddr_sco) ||
573             addr->sa_family != AF_BLUETOOTH)
574                 return -EINVAL;
575
576         lock_sock(sk);
577         if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) {
578                 err = -EBADFD;
579                 goto done;
580         }
581
582         if (sk->sk_type != SOCK_SEQPACKET) {
583                 err = -EINVAL;
584                 goto done;
585         }
586
587         hdev = hci_get_route(&sa->sco_bdaddr, &sco_pi(sk)->src, BDADDR_BREDR);
588         if (!hdev) {
589                 err = -EHOSTUNREACH;
590                 goto done;
591         }
592         hci_dev_lock(hdev);
593
594         /* Set destination address and psm */
595         bacpy(&sco_pi(sk)->dst, &sa->sco_bdaddr);
596
597         err = sco_connect(hdev, sk);
598         hci_dev_unlock(hdev);
599         hci_dev_put(hdev);
600         if (err)
601                 goto done;
602
603         err = bt_sock_wait_state(sk, BT_CONNECTED,
604                                  sock_sndtimeo(sk, flags & O_NONBLOCK));
605
606 done:
607         release_sock(sk);
608         return err;
609 }
610
611 static int sco_sock_listen(struct socket *sock, int backlog)
612 {
613         struct sock *sk = sock->sk;
614         bdaddr_t *src = &sco_pi(sk)->src;
615         int err = 0;
616
617         BT_DBG("sk %p backlog %d", sk, backlog);
618
619         lock_sock(sk);
620
621         if (sk->sk_state != BT_BOUND) {
622                 err = -EBADFD;
623                 goto done;
624         }
625
626         if (sk->sk_type != SOCK_SEQPACKET) {
627                 err = -EINVAL;
628                 goto done;
629         }
630
631         write_lock(&sco_sk_list.lock);
632
633         if (__sco_get_sock_listen_by_addr(src)) {
634                 err = -EADDRINUSE;
635                 goto unlock;
636         }
637
638         sk->sk_max_ack_backlog = backlog;
639         sk->sk_ack_backlog = 0;
640
641         sk->sk_state = BT_LISTEN;
642
643 unlock:
644         write_unlock(&sco_sk_list.lock);
645
646 done:
647         release_sock(sk);
648         return err;
649 }
650
651 static int sco_sock_accept(struct socket *sock, struct socket *newsock,
652                            int flags, bool kern)
653 {
654         DEFINE_WAIT_FUNC(wait, woken_wake_function);
655         struct sock *sk = sock->sk, *ch;
656         long timeo;
657         int err = 0;
658
659         lock_sock(sk);
660
661         timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
662
663         BT_DBG("sk %p timeo %ld", sk, timeo);
664
665         /* Wait for an incoming connection. (wake-one). */
666         add_wait_queue_exclusive(sk_sleep(sk), &wait);
667         while (1) {
668                 if (sk->sk_state != BT_LISTEN) {
669                         err = -EBADFD;
670                         break;
671                 }
672
673                 ch = bt_accept_dequeue(sk, newsock);
674                 if (ch)
675                         break;
676
677                 if (!timeo) {
678                         err = -EAGAIN;
679                         break;
680                 }
681
682                 if (signal_pending(current)) {
683                         err = sock_intr_errno(timeo);
684                         break;
685                 }
686
687                 release_sock(sk);
688
689                 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
690                 lock_sock(sk);
691         }
692         remove_wait_queue(sk_sleep(sk), &wait);
693
694         if (err)
695                 goto done;
696
697         newsock->state = SS_CONNECTED;
698
699         BT_DBG("new socket %p", ch);
700
701 done:
702         release_sock(sk);
703         return err;
704 }
705
706 static int sco_sock_getname(struct socket *sock, struct sockaddr *addr,
707                             int peer)
708 {
709         struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
710         struct sock *sk = sock->sk;
711
712         BT_DBG("sock %p, sk %p", sock, sk);
713
714         addr->sa_family = AF_BLUETOOTH;
715
716         if (peer)
717                 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->dst);
718         else
719                 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->src);
720
721         return sizeof(struct sockaddr_sco);
722 }
723
724 static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg,
725                             size_t len)
726 {
727         struct sock *sk = sock->sk;
728         struct sk_buff *skb;
729         int err;
730
731         BT_DBG("sock %p, sk %p", sock, sk);
732
733         err = sock_error(sk);
734         if (err)
735                 return err;
736
737         if (msg->msg_flags & MSG_OOB)
738                 return -EOPNOTSUPP;
739
740         skb = bt_skb_sendmsg(sk, msg, len, len, 0, 0);
741         if (IS_ERR(skb))
742                 return PTR_ERR(skb);
743
744         lock_sock(sk);
745
746         if (sk->sk_state == BT_CONNECTED)
747                 err = sco_send_frame(sk, skb);
748         else
749                 err = -ENOTCONN;
750
751         release_sock(sk);
752
753         if (err < 0)
754                 kfree_skb(skb);
755         return err;
756 }
757
758 static void sco_conn_defer_accept(struct hci_conn *conn, u16 setting)
759 {
760         struct hci_dev *hdev = conn->hdev;
761
762         BT_DBG("conn %p", conn);
763
764         conn->state = BT_CONFIG;
765
766         if (!lmp_esco_capable(hdev)) {
767                 struct hci_cp_accept_conn_req cp;
768
769                 bacpy(&cp.bdaddr, &conn->dst);
770                 cp.role = 0x00; /* Ignored */
771
772                 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
773         } else {
774                 struct hci_cp_accept_sync_conn_req cp;
775
776                 bacpy(&cp.bdaddr, &conn->dst);
777                 cp.pkt_type = cpu_to_le16(conn->pkt_type);
778
779                 cp.tx_bandwidth   = cpu_to_le32(0x00001f40);
780                 cp.rx_bandwidth   = cpu_to_le32(0x00001f40);
781                 cp.content_format = cpu_to_le16(setting);
782
783                 switch (setting & SCO_AIRMODE_MASK) {
784                 case SCO_AIRMODE_TRANSP:
785                         if (conn->pkt_type & ESCO_2EV3)
786                                 cp.max_latency = cpu_to_le16(0x0008);
787                         else
788                                 cp.max_latency = cpu_to_le16(0x000D);
789                         cp.retrans_effort = 0x02;
790                         break;
791                 case SCO_AIRMODE_CVSD:
792                         cp.max_latency = cpu_to_le16(0xffff);
793                         cp.retrans_effort = 0xff;
794                         break;
795                 default:
796                         /* use CVSD settings as fallback */
797                         cp.max_latency = cpu_to_le16(0xffff);
798                         cp.retrans_effort = 0xff;
799                         break;
800                 }
801
802                 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
803                              sizeof(cp), &cp);
804         }
805 }
806
807 static int sco_sock_recvmsg(struct socket *sock, struct msghdr *msg,
808                             size_t len, int flags)
809 {
810         struct sock *sk = sock->sk;
811         struct sco_pinfo *pi = sco_pi(sk);
812
813         lock_sock(sk);
814
815         if (sk->sk_state == BT_CONNECT2 &&
816             test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
817                 sco_conn_defer_accept(pi->conn->hcon, pi->setting);
818                 sk->sk_state = BT_CONFIG;
819
820                 release_sock(sk);
821                 return 0;
822         }
823
824         release_sock(sk);
825
826         return bt_sock_recvmsg(sock, msg, len, flags);
827 }
828
829 static int sco_sock_setsockopt(struct socket *sock, int level, int optname,
830                                sockptr_t optval, unsigned int optlen)
831 {
832         struct sock *sk = sock->sk;
833         int len, err = 0;
834         struct bt_voice voice;
835         u32 opt;
836
837         BT_DBG("sk %p", sk);
838
839         lock_sock(sk);
840
841         switch (optname) {
842
843         case BT_DEFER_SETUP:
844                 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
845                         err = -EINVAL;
846                         break;
847                 }
848
849                 if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
850                         err = -EFAULT;
851                         break;
852                 }
853
854                 if (opt)
855                         set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
856                 else
857                         clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
858                 break;
859
860         case BT_VOICE:
861                 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
862                     sk->sk_state != BT_CONNECT2) {
863                         err = -EINVAL;
864                         break;
865                 }
866
867                 voice.setting = sco_pi(sk)->setting;
868
869                 len = min_t(unsigned int, sizeof(voice), optlen);
870                 if (copy_from_sockptr(&voice, optval, len)) {
871                         err = -EFAULT;
872                         break;
873                 }
874
875                 /* Explicitly check for these values */
876 #ifdef TIZEN_BT
877                 /* Codec defer accept */
878                 if (voice.setting != (BT_VOICE_TRANSPARENT |
879                     BT_VOICE_CVSD_16BIT) &&
880                     voice.setting != BT_VOICE_CVSD_16BIT) {
881 #else
882                 if (voice.setting != BT_VOICE_TRANSPARENT &&
883                     voice.setting != BT_VOICE_CVSD_16BIT) {
884 #endif
885                         err = -EINVAL;
886                         break;
887                 }
888
889                 sco_pi(sk)->setting = voice.setting;
890                 break;
891
892         case BT_PKT_STATUS:
893                 if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
894                         err = -EFAULT;
895                         break;
896                 }
897
898                 if (opt)
899                         sco_pi(sk)->cmsg_mask |= SCO_CMSG_PKT_STATUS;
900                 else
901                         sco_pi(sk)->cmsg_mask &= SCO_CMSG_PKT_STATUS;
902                 break;
903
904         default:
905                 err = -ENOPROTOOPT;
906                 break;
907         }
908
909         release_sock(sk);
910         return err;
911 }
912
913 static int sco_sock_getsockopt_old(struct socket *sock, int optname,
914                                    char __user *optval, int __user *optlen)
915 {
916         struct sock *sk = sock->sk;
917         struct sco_options opts;
918         struct sco_conninfo cinfo;
919         int len, err = 0;
920
921         BT_DBG("sk %p", sk);
922
923         if (get_user(len, optlen))
924                 return -EFAULT;
925
926         lock_sock(sk);
927
928         switch (optname) {
929         case SCO_OPTIONS:
930                 if (sk->sk_state != BT_CONNECTED &&
931                     !(sk->sk_state == BT_CONNECT2 &&
932                       test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
933                         err = -ENOTCONN;
934                         break;
935                 }
936
937                 opts.mtu = sco_pi(sk)->conn->mtu;
938
939                 BT_DBG("mtu %u", opts.mtu);
940
941                 len = min_t(unsigned int, len, sizeof(opts));
942                 if (copy_to_user(optval, (char *)&opts, len))
943                         err = -EFAULT;
944
945                 break;
946
947         case SCO_CONNINFO:
948                 if (sk->sk_state != BT_CONNECTED &&
949                     !(sk->sk_state == BT_CONNECT2 &&
950                       test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
951                         err = -ENOTCONN;
952                         break;
953                 }
954
955                 memset(&cinfo, 0, sizeof(cinfo));
956                 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
957                 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
958
959                 len = min_t(unsigned int, len, sizeof(cinfo));
960                 if (copy_to_user(optval, (char *)&cinfo, len))
961                         err = -EFAULT;
962
963                 break;
964
965         default:
966                 err = -ENOPROTOOPT;
967                 break;
968         }
969
970         release_sock(sk);
971         return err;
972 }
973
974 static int sco_sock_getsockopt(struct socket *sock, int level, int optname,
975                                char __user *optval, int __user *optlen)
976 {
977         struct sock *sk = sock->sk;
978         int len, err = 0;
979         struct bt_voice voice;
980         u32 phys;
981         int pkt_status;
982
983         BT_DBG("sk %p", sk);
984
985         if (level == SOL_SCO)
986                 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
987
988         if (get_user(len, optlen))
989                 return -EFAULT;
990
991         lock_sock(sk);
992
993         switch (optname) {
994
995         case BT_DEFER_SETUP:
996                 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
997                         err = -EINVAL;
998                         break;
999                 }
1000
1001                 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
1002                              (u32 __user *)optval))
1003                         err = -EFAULT;
1004
1005                 break;
1006
1007         case BT_VOICE:
1008                 voice.setting = sco_pi(sk)->setting;
1009
1010                 len = min_t(unsigned int, len, sizeof(voice));
1011                 if (copy_to_user(optval, (char *)&voice, len))
1012                         err = -EFAULT;
1013
1014                 break;
1015
1016         case BT_PHY:
1017                 if (sk->sk_state != BT_CONNECTED) {
1018                         err = -ENOTCONN;
1019                         break;
1020                 }
1021
1022                 phys = hci_conn_get_phy(sco_pi(sk)->conn->hcon);
1023
1024                 if (put_user(phys, (u32 __user *) optval))
1025                         err = -EFAULT;
1026                 break;
1027
1028         case BT_PKT_STATUS:
1029                 pkt_status = (sco_pi(sk)->cmsg_mask & SCO_CMSG_PKT_STATUS);
1030
1031                 if (put_user(pkt_status, (int __user *)optval))
1032                         err = -EFAULT;
1033                 break;
1034
1035         case BT_SNDMTU:
1036         case BT_RCVMTU:
1037                 if (sk->sk_state != BT_CONNECTED) {
1038                         err = -ENOTCONN;
1039                         break;
1040                 }
1041
1042                 if (put_user(sco_pi(sk)->conn->mtu, (u32 __user *)optval))
1043                         err = -EFAULT;
1044                 break;
1045
1046         default:
1047                 err = -ENOPROTOOPT;
1048                 break;
1049         }
1050
1051         release_sock(sk);
1052         return err;
1053 }
1054
1055 static int sco_sock_shutdown(struct socket *sock, int how)
1056 {
1057         struct sock *sk = sock->sk;
1058         int err = 0;
1059
1060         BT_DBG("sock %p, sk %p", sock, sk);
1061
1062         if (!sk)
1063                 return 0;
1064
1065         sock_hold(sk);
1066         lock_sock(sk);
1067
1068         if (!sk->sk_shutdown) {
1069                 sk->sk_shutdown = SHUTDOWN_MASK;
1070                 sco_sock_clear_timer(sk);
1071                 __sco_sock_close(sk);
1072
1073                 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1074                     !(current->flags & PF_EXITING))
1075                         err = bt_sock_wait_state(sk, BT_CLOSED,
1076                                                  sk->sk_lingertime);
1077         }
1078
1079         release_sock(sk);
1080         sock_put(sk);
1081
1082         return err;
1083 }
1084
1085 static int sco_sock_release(struct socket *sock)
1086 {
1087         struct sock *sk = sock->sk;
1088         int err = 0;
1089
1090         BT_DBG("sock %p, sk %p", sock, sk);
1091
1092         if (!sk)
1093                 return 0;
1094
1095         sco_sock_close(sk);
1096
1097         if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1098             !(current->flags & PF_EXITING)) {
1099                 lock_sock(sk);
1100                 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1101                 release_sock(sk);
1102         }
1103
1104 #ifdef TIZEN_BT
1105         /* SCO kernel panic fix */
1106         bt_sock_unlink(&sco_sk_list, sk);
1107 #endif
1108         sock_orphan(sk);
1109         sco_sock_kill(sk);
1110         return err;
1111 }
1112
1113 static void sco_conn_ready(struct sco_conn *conn)
1114 {
1115         struct sock *parent;
1116         struct sock *sk = conn->sk;
1117
1118         BT_DBG("conn %p", conn);
1119
1120         if (sk) {
1121                 lock_sock(sk);
1122                 sco_sock_clear_timer(sk);
1123                 sk->sk_state = BT_CONNECTED;
1124                 sk->sk_state_change(sk);
1125                 release_sock(sk);
1126         } else {
1127                 sco_conn_lock(conn);
1128
1129                 if (!conn->hcon) {
1130                         sco_conn_unlock(conn);
1131                         return;
1132                 }
1133
1134 #ifdef TIZEN_BT
1135                 /* Multiple SCO accept feature */
1136                 parent = sco_get_sock_listen(&conn->hcon->dst);
1137 #else
1138                 parent = sco_get_sock_listen(&conn->hcon->src);
1139 #endif
1140                 if (!parent) {
1141                         sco_conn_unlock(conn);
1142                         return;
1143                 }
1144
1145                 lock_sock(parent);
1146
1147                 sk = sco_sock_alloc(sock_net(parent), NULL,
1148                                     BTPROTO_SCO, GFP_ATOMIC, 0);
1149                 if (!sk) {
1150                         release_sock(parent);
1151                         sco_conn_unlock(conn);
1152                         return;
1153                 }
1154
1155                 sco_sock_init(sk, parent);
1156
1157                 bacpy(&sco_pi(sk)->src, &conn->hcon->src);
1158                 bacpy(&sco_pi(sk)->dst, &conn->hcon->dst);
1159
1160                 hci_conn_hold(conn->hcon);
1161                 __sco_chan_add(conn, sk, parent);
1162
1163                 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
1164                         sk->sk_state = BT_CONNECT2;
1165                 else
1166                         sk->sk_state = BT_CONNECTED;
1167
1168                 /* Wake up parent */
1169                 parent->sk_data_ready(parent);
1170
1171                 release_sock(parent);
1172
1173                 sco_conn_unlock(conn);
1174         }
1175 }
1176
1177 #ifdef TIZEN_BT
1178 /* Link policy */
1179 static int hci_write_acl_link_policy(struct hci_conn *hcon, uint16_t policy)
1180 {
1181         struct hci_conn *hcon_acl;
1182         struct hci_cp_write_link_policy cp;
1183
1184         BT_DBG("Write link Policy %d", policy);
1185
1186         hcon_acl = hci_conn_hash_lookup_ba(hcon->hdev, ACL_LINK, &hcon->dst);
1187         if (!hcon_acl) {
1188                 BT_ERR("ACL does not alive");
1189                 return -1;
1190         }
1191
1192         memset(&cp, 0, sizeof(cp));
1193         cp.handle = __cpu_to_le16(hcon_acl->handle);
1194         cp.policy = __cpu_to_le16(policy);
1195
1196         hci_send_cmd(hcon_acl->hdev, HCI_OP_WRITE_LINK_POLICY, sizeof(cp), &cp);
1197
1198         return 0;
1199 }
1200
1201 /* WBC/NBC feature */
1202 void sco_connect_set_gw_nbc(struct hci_dev *hdev)
1203 {
1204         struct hci_cp_write_voice_setting cp1;
1205         struct hci_cp_bcm_wbs_req cp2;
1206         struct hci_cp_i2c_pcm_req cp3;
1207         struct hci_cp_sco_pcm_req cp4;
1208
1209         BT_DBG("Setting the NBC params, hdev = %p", hdev);
1210
1211         cp1.voice_setting = cpu_to_le16(0x0060);
1212         hci_send_cmd(hdev, HCI_OP_WRITE_VOICE_SETTING, sizeof(cp1), &cp1);
1213         hdev->voice_setting = cpu_to_le16(0x0060);
1214
1215         cp2.role = 0x00;  /* WbDisable */
1216         cp2.pkt_type = cpu_to_le16(0x0002);
1217         hci_send_cmd(hdev, HCI_BCM_ENABLE_WBS_REQ, sizeof(cp2), &cp2);
1218
1219         cp3.i2c_enable = 0x01;
1220         cp3.is_master = 0x00;
1221         cp3.pcm_rate = 0x00;
1222         cp3.clock_rate = 0x01;
1223         hci_send_cmd(hdev, HCI_BCM_I2C_PCM_REQ, sizeof(cp3), &cp3);
1224
1225         cp4.sco_routing = 0x00;
1226         cp4.pcm_rate = 0x01;
1227         cp4.frame_type = 0x00;
1228         cp4.sync_mode = 0x00;
1229         cp4.clock_mode = 0x00;
1230         hci_send_cmd(hdev, HCI_BCM_SCO_PCM_REQ, sizeof(cp4), &cp4);
1231 }
1232
1233 void sco_connect_set_gw_wbc(struct hci_dev *hdev)
1234 {
1235         struct hci_cp_write_voice_setting cp1;
1236         struct hci_cp_bcm_wbs_req cp2;
1237         struct hci_cp_i2c_pcm_req cp3;
1238         struct hci_cp_sco_pcm_req cp4;
1239
1240         BT_DBG("Setting the WBC params, hdev = %p", hdev);
1241         cp1.voice_setting = cpu_to_le16(0x0003 | 0x0060);
1242         hci_send_cmd(hdev, HCI_OP_WRITE_VOICE_SETTING, sizeof(cp1), &cp1);
1243         hdev->voice_setting = cpu_to_le16(0x0003 | 0x0060);
1244
1245         cp2.role = 0x01; /* Enable */
1246         cp2.pkt_type = cpu_to_le16(0x0002);
1247         hci_send_cmd(hdev, HCI_BCM_ENABLE_WBS_REQ, sizeof(cp2), &cp2);
1248
1249         cp3.i2c_enable = 0x00;
1250         cp3.is_master = 0x00;
1251         cp3.pcm_rate = 0x01;
1252         cp3.clock_rate = 0x02;
1253         hci_send_cmd(hdev, HCI_BCM_I2C_PCM_REQ, sizeof(cp3), &cp3);
1254
1255         cp4.sco_routing = 0x00;
1256         cp4.pcm_rate = 0x00;
1257         cp4.frame_type = 0x00;
1258         cp4.sync_mode = 0x00;
1259         cp4.clock_mode = 0x00;
1260         hci_send_cmd(hdev, HCI_BCM_SCO_PCM_REQ, sizeof(cp4), &cp4);
1261 }
1262
1263 void sco_connect_set_nbc(struct hci_dev *hdev)
1264 {
1265         struct hci_cp_write_voice_setting cp1;
1266         struct hci_cp_bcm_wbs_req cp2;
1267         struct hci_cp_i2c_pcm_req cp3;
1268         struct hci_cp_sco_pcm_req cp4;
1269
1270         BT_DBG("Setting the NBC params, hdev = %p", hdev);
1271
1272         cp1.voice_setting = cpu_to_le16(0x0060);
1273         hci_send_cmd(hdev, HCI_OP_WRITE_VOICE_SETTING, sizeof(cp1), &cp1);
1274         hdev->voice_setting = cpu_to_le16(0x0060);
1275
1276         cp2.role = 0x00; /* WbDisable */
1277         cp2.pkt_type = cpu_to_le16(0x0002);
1278         hci_send_cmd(hdev, HCI_BCM_ENABLE_WBS_REQ, sizeof(cp2), &cp2);
1279
1280         cp3.i2c_enable = 0x00;
1281         cp3.is_master = 0x00;
1282         cp3.pcm_rate = 0x00;
1283         cp3.clock_rate = 0x04;
1284         hci_send_cmd(hdev, HCI_BCM_I2C_PCM_REQ, sizeof(cp3), &cp3);
1285
1286         cp4.sco_routing = 0x00;
1287         cp4.pcm_rate = 0x04;
1288         cp4.frame_type = 0x00;
1289         cp4.sync_mode = 0x00;
1290         cp4.clock_mode = 0x00;
1291         hci_send_cmd(hdev, HCI_BCM_SCO_PCM_REQ, sizeof(cp4), &cp4);
1292 }
1293
1294 void sco_connect_set_wbc(struct hci_dev *hdev)
1295 {
1296         struct hci_cp_write_voice_setting cp1;
1297         struct hci_cp_bcm_wbs_req cp2;
1298         struct hci_cp_i2c_pcm_req cp3;
1299         struct hci_cp_sco_pcm_req cp4;
1300
1301         BT_DBG("Setting the WBC params, hdev = %p", hdev);
1302         cp1.voice_setting = cpu_to_le16(0x0003 | 0x0060);
1303         hci_send_cmd(hdev, HCI_OP_WRITE_VOICE_SETTING, sizeof(cp1), &cp1);
1304         hdev->voice_setting = cpu_to_le16(0x0003 | 0x0060);
1305
1306         cp2.role = 0x01; /* Enable */
1307         cp2.pkt_type = cpu_to_le16(0x0002);
1308         hci_send_cmd(hdev, HCI_BCM_ENABLE_WBS_REQ, sizeof(cp2), &cp2);
1309
1310         cp3.i2c_enable = 0x00;
1311         cp3.is_master = 0x00;
1312         cp3.pcm_rate = 0x01;
1313         cp3.clock_rate = 0x04;
1314         hci_send_cmd(hdev, HCI_BCM_I2C_PCM_REQ, sizeof(cp3), &cp3);
1315
1316         cp4.sco_routing = 0x00;
1317         cp4.pcm_rate = 0x04;
1318         cp4.frame_type = 0x00;
1319         cp4.sync_mode = 0x00;
1320         cp4.clock_mode = 0x00;
1321         hci_send_cmd(hdev, HCI_BCM_SCO_PCM_REQ, sizeof(cp4), &cp4);
1322 }
1323 #endif
1324
1325 /* ----- SCO interface with lower layer (HCI) ----- */
1326 int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
1327 {
1328         struct sock *sk;
1329         int lm = 0;
1330
1331         BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
1332
1333         /* Find listening sockets */
1334         read_lock(&sco_sk_list.lock);
1335         sk_for_each(sk, &sco_sk_list.head) {
1336                 if (sk->sk_state != BT_LISTEN)
1337                         continue;
1338
1339 #ifdef TIZEN_BT
1340                 /* Multiple SCO accept feature */
1341                 if (!bacmp(&sco_pi(sk)->src, bdaddr) ||
1342                     !bacmp(&sco_pi(sk)->src, BDADDR_ANY)) {
1343 #else
1344                 if (!bacmp(&sco_pi(sk)->src, &hdev->bdaddr) ||
1345                     !bacmp(&sco_pi(sk)->src, BDADDR_ANY)) {
1346 #endif
1347                         lm |= HCI_LM_ACCEPT;
1348
1349                         if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
1350                                 *flags |= HCI_PROTO_DEFER;
1351                         break;
1352                 }
1353         }
1354         read_unlock(&sco_sk_list.lock);
1355
1356 #ifdef TIZEN_BT
1357         /* WBC/NBC feature */
1358         if ((lm & HCI_LM_ACCEPT) && !hci_conn_hash_lookup_sco(hdev)) {
1359                 struct hci_conn *hcon_acl;
1360
1361                 hcon_acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, bdaddr);
1362                 if (!hcon_acl) {
1363                         BT_ERR("ACL doesn't alive. Use 0x%X",
1364                                hdev->voice_setting);
1365
1366                         if (hdev->voice_setting == 0x0063)
1367                                 sco_connect_set_wbc(hdev);
1368                         else
1369                                 sco_connect_set_nbc(hdev);
1370                 } else if (hcon_acl->sco_role == MGMT_SCO_ROLE_HANDSFREE) {
1371                         BT_DBG("Handsfree SCO 0x%X", hcon_acl->voice_setting);
1372                         if (hcon_acl->voice_setting == 0x0063)
1373                                 sco_connect_set_wbc(hdev);
1374                         else
1375                                 sco_connect_set_nbc(hdev);
1376                 } else {
1377                         BT_DBG("Gateway SCO 0x%X", hcon_acl->voice_setting);
1378                         if (hcon_acl->voice_setting == 0x0063)
1379                                 sco_connect_set_gw_wbc(hdev);
1380                         else
1381                                 sco_connect_set_gw_nbc(hdev);
1382                 }
1383         }
1384 #endif
1385
1386         return lm;
1387 }
1388
1389 static void sco_connect_cfm(struct hci_conn *hcon, __u8 status)
1390 {
1391         if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1392                 return;
1393
1394         BT_DBG("hcon %p bdaddr %pMR status %u", hcon, &hcon->dst, status);
1395
1396         if (!status) {
1397                 struct sco_conn *conn;
1398
1399                 conn = sco_conn_add(hcon);
1400                 if (conn)
1401                         sco_conn_ready(conn);
1402
1403 #ifdef TIZEN_BT
1404                 /* Link policy */
1405                 hci_write_acl_link_policy(hcon, HCI_LP_RSWITCH);
1406 #endif
1407         } else
1408                 sco_conn_del(hcon, bt_to_errno(status));
1409 }
1410
1411 static void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
1412 {
1413         if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1414                 return;
1415
1416         BT_DBG("hcon %p reason %d", hcon, reason);
1417
1418 #ifdef TIZEN_BT
1419         /* Link policy */
1420         hci_write_acl_link_policy(hcon, HCI_LP_SNIFF | HCI_LP_RSWITCH);
1421 #endif
1422
1423         sco_conn_del(hcon, bt_to_errno(reason));
1424 }
1425
1426 void sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
1427 {
1428         struct sco_conn *conn = hcon->sco_data;
1429
1430         if (!conn)
1431                 goto drop;
1432
1433         BT_DBG("conn %p len %u", conn, skb->len);
1434
1435         if (skb->len) {
1436                 sco_recv_frame(conn, skb);
1437                 return;
1438         }
1439
1440 drop:
1441         kfree_skb(skb);
1442 }
1443
1444 static struct hci_cb sco_cb = {
1445         .name           = "SCO",
1446         .connect_cfm    = sco_connect_cfm,
1447         .disconn_cfm    = sco_disconn_cfm,
1448 };
1449
1450 static int sco_debugfs_show(struct seq_file *f, void *p)
1451 {
1452         struct sock *sk;
1453
1454         read_lock(&sco_sk_list.lock);
1455
1456         sk_for_each(sk, &sco_sk_list.head) {
1457                 seq_printf(f, "%pMR %pMR %d\n", &sco_pi(sk)->src,
1458                            &sco_pi(sk)->dst, sk->sk_state);
1459         }
1460
1461         read_unlock(&sco_sk_list.lock);
1462
1463         return 0;
1464 }
1465
1466 DEFINE_SHOW_ATTRIBUTE(sco_debugfs);
1467
1468 static struct dentry *sco_debugfs;
1469
1470 static const struct proto_ops sco_sock_ops = {
1471         .family         = PF_BLUETOOTH,
1472         .owner          = THIS_MODULE,
1473         .release        = sco_sock_release,
1474         .bind           = sco_sock_bind,
1475         .connect        = sco_sock_connect,
1476         .listen         = sco_sock_listen,
1477         .accept         = sco_sock_accept,
1478         .getname        = sco_sock_getname,
1479         .sendmsg        = sco_sock_sendmsg,
1480         .recvmsg        = sco_sock_recvmsg,
1481         .poll           = bt_sock_poll,
1482         .ioctl          = bt_sock_ioctl,
1483         .gettstamp      = sock_gettstamp,
1484         .mmap           = sock_no_mmap,
1485         .socketpair     = sock_no_socketpair,
1486         .shutdown       = sco_sock_shutdown,
1487         .setsockopt     = sco_sock_setsockopt,
1488         .getsockopt     = sco_sock_getsockopt
1489 };
1490
1491 static const struct net_proto_family sco_sock_family_ops = {
1492         .family = PF_BLUETOOTH,
1493         .owner  = THIS_MODULE,
1494         .create = sco_sock_create,
1495 };
1496
1497 int __init sco_init(void)
1498 {
1499         int err;
1500
1501         BUILD_BUG_ON(sizeof(struct sockaddr_sco) > sizeof(struct sockaddr));
1502
1503         err = proto_register(&sco_proto, 0);
1504         if (err < 0)
1505                 return err;
1506
1507         err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1508         if (err < 0) {
1509                 BT_ERR("SCO socket registration failed");
1510                 goto error;
1511         }
1512
1513         err = bt_procfs_init(&init_net, "sco", &sco_sk_list, NULL);
1514         if (err < 0) {
1515                 BT_ERR("Failed to create SCO proc file");
1516                 bt_sock_unregister(BTPROTO_SCO);
1517                 goto error;
1518         }
1519
1520         BT_INFO("SCO socket layer initialized");
1521
1522         hci_register_cb(&sco_cb);
1523
1524         if (IS_ERR_OR_NULL(bt_debugfs))
1525                 return 0;
1526
1527         sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs,
1528                                           NULL, &sco_debugfs_fops);
1529
1530         return 0;
1531
1532 error:
1533         proto_unregister(&sco_proto);
1534         return err;
1535 }
1536
1537 void sco_exit(void)
1538 {
1539         bt_procfs_cleanup(&init_net, "sco");
1540
1541         debugfs_remove(sco_debugfs);
1542
1543         hci_unregister_cb(&sco_cb);
1544
1545         bt_sock_unregister(BTPROTO_SCO);
1546
1547         proto_unregister(&sco_proto);
1548 }
1549
1550 module_param(disable_esco, bool, 0644);
1551 MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");