[Bluetooth] Handle return values from driver core functions
[pandora-kernel.git] / net / bluetooth / l2cap.c
index 59b2dd3..66fc621 100644 (file)
 
 /* Bluetooth L2CAP core and sockets. */
 
-#include <linux/config.h>
 #include <linux/module.h>
 
 #include <linux/types.h>
+#include <linux/capability.h>
 #include <linux/errno.h>
 #include <linux/kernel.h>
 #include <linux/sched.h>
@@ -38,9 +38,8 @@
 #include <linux/interrupt.h>
 #include <linux/socket.h>
 #include <linux/skbuff.h>
-#include <linux/proc_fs.h>
-#include <linux/seq_file.h>
 #include <linux/list.h>
+#include <linux/device.h>
 #include <net/sock.h>
 
 #include <asm/system.h>
 #define BT_DBG(D...)
 #endif
 
-#define VERSION "2.7"
+#define VERSION "2.8"
 
-static struct proto_ops l2cap_sock_ops;
+static const struct proto_ops l2cap_sock_ops;
 
 static struct bt_sock_list l2cap_sk_list = {
        .lock = RW_LOCK_UNLOCKED
 };
 
-static int l2cap_conn_del(struct hci_conn *conn, int err);
-
-static void __l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk, struct sock *parent);
-static void l2cap_chan_del(struct sock *sk, int err);
-
 static void __l2cap_sock_close(struct sock *sk, int reason);
 static void l2cap_sock_close(struct sock *sk);
 static void l2cap_sock_kill(struct sock *sk);
@@ -110,24 +104,177 @@ static void l2cap_sock_init_timer(struct sock *sk)
        sk->sk_timer.data = (unsigned long)sk;
 }
 
+/* ---- L2CAP channels ---- */
+static struct sock *__l2cap_get_chan_by_dcid(struct l2cap_chan_list *l, u16 cid)
+{
+       struct sock *s;
+       for (s = l->head; s; s = l2cap_pi(s)->next_c) {
+               if (l2cap_pi(s)->dcid == cid)
+                       break;
+       }
+       return s;
+}
+
+static struct sock *__l2cap_get_chan_by_scid(struct l2cap_chan_list *l, u16 cid)
+{
+       struct sock *s;
+       for (s = l->head; s; s = l2cap_pi(s)->next_c) {
+               if (l2cap_pi(s)->scid == cid)
+                       break;
+       }
+       return s;
+}
+
+/* Find channel with given SCID.
+ * Returns locked socket */
+static inline struct sock *l2cap_get_chan_by_scid(struct l2cap_chan_list *l, u16 cid)
+{
+       struct sock *s;
+       read_lock(&l->lock);
+       s = __l2cap_get_chan_by_scid(l, cid);
+       if (s) bh_lock_sock(s);
+       read_unlock(&l->lock);
+       return s;
+}
+
+static struct sock *__l2cap_get_chan_by_ident(struct l2cap_chan_list *l, u8 ident)
+{
+       struct sock *s;
+       for (s = l->head; s; s = l2cap_pi(s)->next_c) {
+               if (l2cap_pi(s)->ident == ident)
+                       break;
+       }
+       return s;
+}
+
+static inline struct sock *l2cap_get_chan_by_ident(struct l2cap_chan_list *l, u8 ident)
+{
+       struct sock *s;
+       read_lock(&l->lock);
+       s = __l2cap_get_chan_by_ident(l, ident);
+       if (s) bh_lock_sock(s);
+       read_unlock(&l->lock);
+       return s;
+}
+
+static u16 l2cap_alloc_cid(struct l2cap_chan_list *l)
+{
+       u16 cid = 0x0040;
+
+       for (; cid < 0xffff; cid++) {
+               if(!__l2cap_get_chan_by_scid(l, cid))
+                       return cid;
+       }
+
+       return 0;
+}
+
+static inline void __l2cap_chan_link(struct l2cap_chan_list *l, struct sock *sk)
+{
+       sock_hold(sk);
+
+       if (l->head)
+               l2cap_pi(l->head)->prev_c = sk;
+
+       l2cap_pi(sk)->next_c = l->head;
+       l2cap_pi(sk)->prev_c = NULL;
+       l->head = sk;
+}
+
+static inline void l2cap_chan_unlink(struct l2cap_chan_list *l, struct sock *sk)
+{
+       struct sock *next = l2cap_pi(sk)->next_c, *prev = l2cap_pi(sk)->prev_c;
+
+       write_lock_bh(&l->lock);
+       if (sk == l->head)
+               l->head = next;
+
+       if (next)
+               l2cap_pi(next)->prev_c = prev;
+       if (prev)
+               l2cap_pi(prev)->next_c = next;
+       write_unlock_bh(&l->lock);
+
+       __sock_put(sk);
+}
+
+static void __l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk, struct sock *parent)
+{
+       struct l2cap_chan_list *l = &conn->chan_list;
+
+       BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, l2cap_pi(sk)->psm, l2cap_pi(sk)->dcid);
+
+       l2cap_pi(sk)->conn = conn;
+
+       if (sk->sk_type == SOCK_SEQPACKET) {
+               /* Alloc CID for connection-oriented socket */
+               l2cap_pi(sk)->scid = l2cap_alloc_cid(l);
+       } else if (sk->sk_type == SOCK_DGRAM) {
+               /* Connectionless socket */
+               l2cap_pi(sk)->scid = 0x0002;
+               l2cap_pi(sk)->dcid = 0x0002;
+               l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU;
+       } else {
+               /* Raw socket can send/recv signalling messages only */
+               l2cap_pi(sk)->scid = 0x0001;
+               l2cap_pi(sk)->dcid = 0x0001;
+               l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU;
+       }
+
+       __l2cap_chan_link(l, sk);
+
+       if (parent)
+               bt_accept_enqueue(parent, sk);
+}
+
+/* Delete channel. 
+ * Must be called on the locked socket. */
+static void l2cap_chan_del(struct sock *sk, int err)
+{
+       struct l2cap_conn *conn = l2cap_pi(sk)->conn;
+       struct sock *parent = bt_sk(sk)->parent;
+
+       l2cap_sock_clear_timer(sk);
+
+       BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
+
+       if (conn) { 
+               /* Unlink from channel list */
+               l2cap_chan_unlink(&conn->chan_list, sk);
+               l2cap_pi(sk)->conn = NULL;
+               hci_conn_put(conn->hcon);
+       }
+
+       sk->sk_state  = BT_CLOSED;
+       sock_set_flag(sk, SOCK_ZAPPED);
+
+       if (err)
+               sk->sk_err = err;
+
+       if (parent) {
+               bt_accept_unlink(sk);
+               parent->sk_data_ready(parent, 0);
+       } else
+               sk->sk_state_change(sk);
+}
+
 /* ---- L2CAP connections ---- */
 static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
 {
-       struct l2cap_conn *conn;
-
-       if ((conn = hcon->l2cap_data))
-               return conn;
+       struct l2cap_conn *conn = hcon->l2cap_data;
 
-       if (status)
+       if (conn || status)
                return conn;
 
-       if (!(conn = kmalloc(sizeof(struct l2cap_conn), GFP_ATOMIC)))
+       conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC);
+       if (!conn)
                return NULL;
-       memset(conn, 0, sizeof(struct l2cap_conn));
 
        hcon->l2cap_data = conn;
        conn->hcon = hcon;
 
+       BT_DBG("hcon %p conn %p", hcon, conn);
+
        conn->mtu = hcon->hdev->acl_mtu;
        conn->src = &hcon->hdev->bdaddr;
        conn->dst = &hcon->dst;
@@ -135,17 +282,16 @@ static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
        spin_lock_init(&conn->lock);
        rwlock_init(&conn->chan_list.lock);
 
-       BT_DBG("hcon %p conn %p", hcon, conn);
        return conn;
 }
 
-static int l2cap_conn_del(struct hci_conn *hcon, int err)
+static void l2cap_conn_del(struct hci_conn *hcon, int err)
 {
-       struct l2cap_conn *conn;
+       struct l2cap_conn *conn = hcon->l2cap_data;
        struct sock *sk;
 
-       if (!(conn = hcon->l2cap_data)) 
-               return 0;
+       if (!conn)
+               return;
 
        BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
 
@@ -162,15 +308,14 @@ static int l2cap_conn_del(struct hci_conn *hcon, int err)
 
        hcon->l2cap_data = NULL;
        kfree(conn);
-       return 0;
 }
 
 static inline void l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk, struct sock *parent)
 {
        struct l2cap_chan_list *l = &conn->chan_list;
-       write_lock(&l->lock);
+       write_lock_bh(&l->lock);
        __l2cap_chan_add(conn, sk, parent);
-       write_unlock(&l->lock);
+       write_unlock_bh(&l->lock);
 }
 
 static inline u8 l2cap_get_ident(struct l2cap_conn *conn)
@@ -183,14 +328,14 @@ static inline u8 l2cap_get_ident(struct l2cap_conn *conn)
         *  200 - 254 are used by utilities like l2ping, etc.
         */
 
-       spin_lock(&conn->lock);
+       spin_lock_bh(&conn->lock);
 
        if (++conn->tx_ident > 128)
                conn->tx_ident = 1;
 
        id = conn->tx_ident;
 
-       spin_unlock(&conn->lock);
+       spin_unlock_bh(&conn->lock);
 
        return id;
 }
@@ -768,8 +913,9 @@ static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct ms
 
        BT_DBG("sock %p, sk %p", sock, sk);
 
-       if (sk->sk_err)
-               return sock_error(sk);
+       err = sock_error(sk);
+       if (err)
+               return err;
 
        if (msg->msg_flags & MSG_OOB)
                return -EOPNOTSUPP;
@@ -925,160 +1071,6 @@ static int l2cap_sock_release(struct socket *sock)
        return err;
 }
 
-/* ---- L2CAP channels ---- */
-static struct sock *__l2cap_get_chan_by_dcid(struct l2cap_chan_list *l, u16 cid)
-{
-       struct sock *s;
-       for (s = l->head; s; s = l2cap_pi(s)->next_c) {
-               if (l2cap_pi(s)->dcid == cid)
-                       break;
-       }
-       return s;
-}
-
-static struct sock *__l2cap_get_chan_by_scid(struct l2cap_chan_list *l, u16 cid)
-{
-       struct sock *s;
-       for (s = l->head; s; s = l2cap_pi(s)->next_c) {
-               if (l2cap_pi(s)->scid == cid)
-                       break;
-       }
-       return s;
-}
-
-/* Find channel with given SCID.
- * Returns locked socket */
-static inline struct sock *l2cap_get_chan_by_scid(struct l2cap_chan_list *l, u16 cid)
-{
-       struct sock *s;
-       read_lock(&l->lock);
-       s = __l2cap_get_chan_by_scid(l, cid);
-       if (s) bh_lock_sock(s);
-       read_unlock(&l->lock);
-       return s;
-}
-
-static struct sock *__l2cap_get_chan_by_ident(struct l2cap_chan_list *l, u8 ident)
-{
-       struct sock *s;
-       for (s = l->head; s; s = l2cap_pi(s)->next_c) {
-               if (l2cap_pi(s)->ident == ident)
-                       break;
-       }
-       return s;
-}
-
-static inline struct sock *l2cap_get_chan_by_ident(struct l2cap_chan_list *l, u8 ident)
-{
-       struct sock *s;
-       read_lock(&l->lock);
-       s = __l2cap_get_chan_by_ident(l, ident);
-       if (s) bh_lock_sock(s);
-       read_unlock(&l->lock);
-       return s;
-}
-
-static u16 l2cap_alloc_cid(struct l2cap_chan_list *l)
-{
-       u16 cid = 0x0040;
-
-       for (; cid < 0xffff; cid++) {
-               if(!__l2cap_get_chan_by_scid(l, cid))
-                       return cid;
-       }
-
-       return 0;
-}
-
-static inline void __l2cap_chan_link(struct l2cap_chan_list *l, struct sock *sk)
-{
-       sock_hold(sk);
-
-       if (l->head)
-               l2cap_pi(l->head)->prev_c = sk;
-
-       l2cap_pi(sk)->next_c = l->head;
-       l2cap_pi(sk)->prev_c = NULL;
-       l->head = sk;
-}
-
-static inline void l2cap_chan_unlink(struct l2cap_chan_list *l, struct sock *sk)
-{
-       struct sock *next = l2cap_pi(sk)->next_c, *prev = l2cap_pi(sk)->prev_c;
-
-       write_lock(&l->lock);
-       if (sk == l->head)
-               l->head = next;
-
-       if (next)
-               l2cap_pi(next)->prev_c = prev;
-       if (prev)
-               l2cap_pi(prev)->next_c = next;
-       write_unlock(&l->lock);
-
-       __sock_put(sk);
-}
-
-static void __l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk, struct sock *parent)
-{
-       struct l2cap_chan_list *l = &conn->chan_list;
-
-       BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, l2cap_pi(sk)->psm, l2cap_pi(sk)->dcid);
-
-       l2cap_pi(sk)->conn = conn;
-
-       if (sk->sk_type == SOCK_SEQPACKET) {
-               /* Alloc CID for connection-oriented socket */
-               l2cap_pi(sk)->scid = l2cap_alloc_cid(l);
-       } else if (sk->sk_type == SOCK_DGRAM) {
-               /* Connectionless socket */
-               l2cap_pi(sk)->scid = 0x0002;
-               l2cap_pi(sk)->dcid = 0x0002;
-               l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU;
-       } else {
-               /* Raw socket can send/recv signalling messages only */
-               l2cap_pi(sk)->scid = 0x0001;
-               l2cap_pi(sk)->dcid = 0x0001;
-               l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU;
-       }
-
-       __l2cap_chan_link(l, sk);
-
-       if (parent)
-               bt_accept_enqueue(parent, sk);
-}
-
-/* Delete channel. 
- * Must be called on the locked socket. */
-static void l2cap_chan_del(struct sock *sk, int err)
-{
-       struct l2cap_conn *conn = l2cap_pi(sk)->conn;
-       struct sock *parent = bt_sk(sk)->parent;
-
-       l2cap_sock_clear_timer(sk);
-
-       BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
-
-       if (conn) { 
-               /* Unlink from channel list */
-               l2cap_chan_unlink(&conn->chan_list, sk);
-               l2cap_pi(sk)->conn = NULL;
-               hci_conn_put(conn->hcon);
-       }
-
-       sk->sk_state  = BT_CLOSED;
-       sock_set_flag(sk, SOCK_ZAPPED);
-
-       if (err)
-               sk->sk_err = err;
-
-       if (parent) {
-               bt_accept_unlink(sk);
-               parent->sk_data_ready(parent, 0);
-       } else
-               sk->sk_state_change(sk);
-}
-
 static void l2cap_conn_ready(struct l2cap_conn *conn)
 {
        struct l2cap_chan_list *l = &conn->chan_list;
@@ -1424,11 +1416,11 @@ static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hd
        if (!sk)
                goto response;
 
-       write_lock(&list->lock);
+       write_lock_bh(&list->lock);
 
        /* Check if we already have channel with that dcid */
        if (__l2cap_get_chan_by_dcid(list, scid)) {
-               write_unlock(&list->lock);
+               write_unlock_bh(&list->lock);
                sock_set_flag(sk, SOCK_ZAPPED);
                l2cap_sock_kill(sk);
                goto response;
@@ -1466,7 +1458,7 @@ static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hd
        result = status = 0;
 
 done:
-       write_unlock(&list->lock);
+       write_unlock_bh(&list->lock);
 
 response:
        bh_unlock_sock(parent);
@@ -1834,7 +1826,9 @@ drop:
        kfree_skb(skb);
 
 done:
-       if (sk) bh_unlock_sock(sk);
+       if (sk)
+               bh_unlock_sock(sk);
+
        return 0;
 }
 
@@ -1925,18 +1919,18 @@ static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
 
 static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
 {
+       struct l2cap_conn *conn;
+
        BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
 
        if (hcon->type != ACL_LINK)
                return 0;
 
        if (!status) {
-               struct l2cap_conn *conn;
-
                conn = l2cap_conn_add(hcon, status);
                if (conn)
                        l2cap_conn_ready(conn);
-       } else 
+       } else
                l2cap_conn_del(hcon, bt_err(status));
 
        return 0;
@@ -1950,19 +1944,21 @@ static int l2cap_disconn_ind(struct hci_conn *hcon, u8 reason)
                return 0;
 
        l2cap_conn_del(hcon, bt_err(reason));
+
        return 0;
 }
 
 static int l2cap_auth_cfm(struct hci_conn *hcon, u8 status)
 {
        struct l2cap_chan_list *l;
-       struct l2cap_conn *conn;
+       struct l2cap_conn *conn = conn = hcon->l2cap_data;
        struct l2cap_conn_rsp rsp;
        struct sock *sk;
        int result;
 
-       if (!(conn = hcon->l2cap_data))
+       if (!conn)
                return 0;
+
        l = &conn->chan_list;
 
        BT_DBG("conn %p", conn);
@@ -2005,13 +2001,14 @@ static int l2cap_auth_cfm(struct hci_conn *hcon, u8 status)
 static int l2cap_encrypt_cfm(struct hci_conn *hcon, u8 status)
 {
        struct l2cap_chan_list *l;
-       struct l2cap_conn *conn;
+       struct l2cap_conn *conn = hcon->l2cap_data;
        struct l2cap_conn_rsp rsp;
        struct sock *sk;
        int result;
 
-       if (!(conn = hcon->l2cap_data))
+       if (!conn)
                return 0;
+
        l = &conn->chan_list;
 
        BT_DBG("conn %p", conn);
@@ -2137,96 +2134,31 @@ drop:
        return 0;
 }
 
-/* ---- Proc fs support ---- */
-#ifdef CONFIG_PROC_FS
-static void *l2cap_seq_start(struct seq_file *seq, loff_t *pos)
+static ssize_t l2cap_sysfs_show(struct class *dev, char *buf)
 {
        struct sock *sk;
        struct hlist_node *node;
-       loff_t l = *pos;
+       char *str = buf;
 
        read_lock_bh(&l2cap_sk_list.lock);
 
-       sk_for_each(sk, node, &l2cap_sk_list.head)
-               if (!l--)
-                       goto found;
-       sk = NULL;
-found:
-       return sk;
-}
+       sk_for_each(sk, node, &l2cap_sk_list.head) {
+               struct l2cap_pinfo *pi = l2cap_pi(sk);
 
-static void *l2cap_seq_next(struct seq_file *seq, void *e, loff_t *pos)
-{
-       (*pos)++;
-       return sk_next(e);
-}
+               str += sprintf(str, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d 0x%x\n",
+                               batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst),
+                               sk->sk_state, pi->psm, pi->scid, pi->dcid, pi->imtu,
+                               pi->omtu, pi->link_mode);
+       }
 
-static void l2cap_seq_stop(struct seq_file *seq, void *e)
-{
        read_unlock_bh(&l2cap_sk_list.lock);
-}
-
-static int  l2cap_seq_show(struct seq_file *seq, void *e)
-{
-       struct sock *sk = e;
-       struct l2cap_pinfo *pi = l2cap_pi(sk);
-
-       seq_printf(seq, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d 0x%x\n",
-                       batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst), 
-                       sk->sk_state, pi->psm, pi->scid, pi->dcid, pi->imtu,
-                       pi->omtu, pi->link_mode);
-       return 0;
-}
 
-static struct seq_operations l2cap_seq_ops = {
-       .start  = l2cap_seq_start,
-       .next   = l2cap_seq_next,
-       .stop   = l2cap_seq_stop,
-       .show   = l2cap_seq_show 
-};
-
-static int l2cap_seq_open(struct inode *inode, struct file *file)
-{
-       return seq_open(file, &l2cap_seq_ops);
+       return (str - buf);
 }
 
-static struct file_operations l2cap_seq_fops = {
-       .owner          = THIS_MODULE,
-       .open           = l2cap_seq_open,
-       .read           = seq_read,
-       .llseek         = seq_lseek,
-       .release        = seq_release,
-};
-
-static int __init l2cap_proc_init(void)
-{
-       struct proc_dir_entry *p = create_proc_entry("l2cap", S_IRUGO, proc_bt);
-       if (!p)
-               return -ENOMEM;
-       p->owner     = THIS_MODULE;
-       p->proc_fops = &l2cap_seq_fops;
-       return 0;
-}
+static CLASS_ATTR(l2cap, S_IRUGO, l2cap_sysfs_show, NULL);
 
-static void __exit l2cap_proc_cleanup(void)
-{
-       remove_proc_entry("l2cap", proc_bt);
-}
-
-#else /* CONFIG_PROC_FS */
-
-static int __init l2cap_proc_init(void)
-{
-       return 0;
-}
-
-static void __exit l2cap_proc_cleanup(void)
-{
-       return;
-}
-#endif /* CONFIG_PROC_FS */
-
-static struct proto_ops l2cap_sock_ops = {
+static const struct proto_ops l2cap_sock_ops = {
        .family         = PF_BLUETOOTH,
        .owner          = THIS_MODULE,
        .release        = l2cap_sock_release,
@@ -2266,7 +2198,7 @@ static struct hci_proto l2cap_hci_proto = {
 static int __init l2cap_init(void)
 {
        int err;
-       
+
        err = proto_register(&l2cap_proto, 0);
        if (err < 0)
                return err;
@@ -2284,7 +2216,8 @@ static int __init l2cap_init(void)
                goto error;
        }
 
-       l2cap_proc_init();
+       if (class_create_file(bt_class, &class_attr_l2cap) < 0)
+               BT_ERR("Failed to create L2CAP info file");
 
        BT_INFO("L2CAP ver %s", VERSION);
        BT_INFO("L2CAP socket layer initialized");
@@ -2298,7 +2231,7 @@ error:
 
 static void __exit l2cap_exit(void)
 {
-       l2cap_proc_cleanup();
+       class_remove_file(bt_class, &class_attr_l2cap);
 
        if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
                BT_ERR("L2CAP socket unregistration failed");