From 1c0fbc2d1b56e0445812c15f24a6a9bf902efe70 Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Thu, 17 Mar 2011 12:17:30 +0100 Subject: bluetooth: Add SCO parameters support for socket Adds ability to setup SCO connection parameters through socket option. ST-Ericsson Linux next: Not tested, ER 256277 ST-Ericsson ID: 256277 ST-Ericsson FOSS-OUT ID: STETL-FOSS-OUT-10137 Change-Id: If859341019fd9f7cc7c0efe95750779e84fac801 Signed-off-by: Andrzej Kaczmarek Reviewed-on: http://gerrit.lud.stericsson.com/gerrit/30136 Reviewed-by: Par-Gunnar HJALMDAHL Tested-by: Par-Gunnar HJALMDAHL Reviewed-by: Srinidhi KASAGAR --- net/bluetooth/hci_conn.c | 70 ++++++++++++++++++++++++----------------- net/bluetooth/hci_event.c | 13 ++++---- net/bluetooth/l2cap_core.c | 8 ++--- net/bluetooth/lib.c | 2 ++ net/bluetooth/mgmt.c | 2 +- net/bluetooth/sco.c | 78 +++++++++++++++++++++++++++++++++++++++++----- 6 files changed, 124 insertions(+), 49 deletions(-) (limited to 'net') diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c index 5ec0db42316..40248744a86 100644 --- a/net/bluetooth/hci_conn.c +++ b/net/bluetooth/hci_conn.c @@ -44,6 +44,7 @@ #include #include +#include static void hci_le_connect(struct hci_conn *conn) { @@ -147,16 +148,21 @@ void hci_add_sco(struct hci_conn *conn, __u16 handle) { struct hci_dev *hdev = conn->hdev; struct hci_cp_add_sco cp; + struct bt_sco_parameters *p = conn->sco_parameters; + __u16 pkt_type; BT_DBG("%p", conn); + /* HCI_Add_SCO_Connection uses shifted bitmask for packet type */ + pkt_type = (p->pkt_type << 5) & conn->pkt_type; + conn->state = BT_CONNECT; conn->out = 1; conn->attempt++; cp.handle = cpu_to_le16(handle); - cp.pkt_type = cpu_to_le16(conn->pkt_type); + cp.pkt_type = cpu_to_le16(pkt_type); hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp); } @@ -165,22 +171,35 @@ void hci_setup_sync(struct hci_conn *conn, __u16 handle) { struct hci_dev *hdev = conn->hdev; struct hci_cp_setup_sync_conn cp; + struct bt_sco_parameters *p = conn->sco_parameters; + __u16 voice_setting; + __u16 pkt_type; BT_DBG("%p", conn); + /* + * Combine voice setting using device parameters and air coding + * format set by user. + */ + voice_setting = (hdev->voice_setting & 0xfffc) | + (p->voice_setting & 0x0003); + + /* Bits for EDR packets have inverted logic in BT spec. */ + pkt_type = (p->pkt_type & conn->pkt_type) ^ EDR_ESCO_MASK; + conn->state = BT_CONNECT; conn->out = 1; conn->attempt++; cp.handle = cpu_to_le16(handle); - cp.pkt_type = cpu_to_le16(conn->pkt_type); - cp.tx_bandwidth = cpu_to_le32(0x00001f40); - cp.rx_bandwidth = cpu_to_le32(0x00001f40); - cp.max_latency = cpu_to_le16(0xffff); - cp.voice_setting = cpu_to_le16(hdev->voice_setting); - cp.retrans_effort = 0xff; + cp.tx_bandwidth = cpu_to_le32(p->tx_bandwidth); + cp.rx_bandwidth = cpu_to_le32(p->rx_bandwidth); + cp.max_latency = cpu_to_le16(p->max_latency); + cp.voice_setting = cpu_to_le16(voice_setting); + cp.retrans_effort = p->retrans_effort; + cp.pkt_type = cpu_to_le16(pkt_type); hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp); } @@ -334,7 +353,7 @@ static void hci_conn_auto_accept(unsigned long arg) } struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, - __u16 pkt_type, bdaddr_t *dst) + bdaddr_t *dst) { struct hci_conn *conn; @@ -362,22 +381,13 @@ struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK; break; case SCO_LINK: - if (!pkt_type) - pkt_type = SCO_ESCO_MASK; + if (lmp_esco_capable(hdev)) + conn->pkt_type = hdev->esco_type & SCO_ESCO_MASK; + else + conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK; + break; case ESCO_LINK: - if (!pkt_type) - pkt_type = ALL_ESCO_MASK; - if (lmp_esco_capable(hdev)) { - /* HCI Setup Synchronous Connection Command uses - reverse logic on the EDR_ESCO_MASK bits */ - conn->pkt_type = (pkt_type ^ EDR_ESCO_MASK) & - hdev->esco_type; - } else { - /* Legacy HCI Add Sco Connection Command uses a - shifted bitmask */ - conn->pkt_type = (pkt_type << 5) & hdev->pkt_type & - SCO_PTYPE_MASK; - } + conn->pkt_type = hdev->esco_type; break; } @@ -501,9 +511,9 @@ EXPORT_SYMBOL(hci_get_route); /* Create SCO, ACL or LE connection. * Device _must_ be locked */ -struct hci_conn *hci_connect(struct hci_dev *hdev, int type, - __u16 pkt_type, bdaddr_t *dst, - __u8 sec_level, __u8 auth_type) +struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, + __u8 sec_level, __u8 auth_type, + struct bt_sco_parameters *sco_parameters) { struct hci_conn *acl; struct hci_conn *sco; @@ -522,7 +532,7 @@ struct hci_conn *hci_connect(struct hci_dev *hdev, int type, if (!entry) return ERR_PTR(-EHOSTUNREACH); - le = hci_conn_add(hdev, LE_LINK, 0, dst); + le = hci_conn_add(hdev, LE_LINK, dst); if (!le) return ERR_PTR(-ENOMEM); @@ -537,7 +547,7 @@ struct hci_conn *hci_connect(struct hci_dev *hdev, int type, acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); if (!acl) { - acl = hci_conn_add(hdev, ACL_LINK, 0, dst); + acl = hci_conn_add(hdev, ACL_LINK, dst); if (!acl) return NULL; } @@ -556,7 +566,7 @@ struct hci_conn *hci_connect(struct hci_dev *hdev, int type, sco = hci_conn_hash_lookup_ba(hdev, type, dst); if (!sco) { - sco = hci_conn_add(hdev, type, pkt_type, dst); + sco = hci_conn_add(hdev, type, dst); if (!sco) { hci_conn_put(acl); return NULL; @@ -568,6 +578,8 @@ struct hci_conn *hci_connect(struct hci_dev *hdev, int type, hci_conn_hold(sco); + sco->sco_parameters = sco_parameters; + if (acl->state == BT_CONNECTED && (sco->state == BT_OPEN || sco->state == BT_CLOSED)) { acl->power_save = 1; diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c index 6cddd03cf8c..3b6b2cefa62 100644 --- a/net/bluetooth/hci_event.c +++ b/net/bluetooth/hci_event.c @@ -993,7 +993,7 @@ static inline void hci_cs_create_conn(struct hci_dev *hdev, __u8 status) } } else { if (!conn) { - conn = hci_conn_add(hdev, ACL_LINK, 0, &cp->bdaddr); + conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr); if (conn) { conn->out = 1; conn->link_mode |= HCI_LM_MASTER; @@ -1316,7 +1316,7 @@ static void hci_cs_le_create_conn(struct hci_dev *hdev, __u8 status) } } else { if (!conn) { - conn = hci_conn_add(hdev, LE_LINK, 0, &cp->peer_addr); + conn = hci_conn_add(hdev, LE_LINK, &cp->peer_addr); if (conn) { conn->dst_type = cp->peer_addr_type; conn->out = 1; @@ -1488,7 +1488,7 @@ static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *sk conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr); if (!conn) { /* pkt_type not yet used for incoming connections */ - conn = hci_conn_add(hdev, ev->link_type, 0, &ev->bdaddr); + conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr); if (!conn) { BT_ERR("No memory for new connection"); hci_dev_unlock(hdev); @@ -2486,9 +2486,8 @@ static inline void hci_sync_conn_complete_evt(struct hci_dev *hdev, struct sk_bu case 0x1c: /* SCO interval rejected */ case 0x1a: /* Unsupported Remote Feature */ case 0x1f: /* Unspecified error */ - if (conn->out && conn->attempt < 2) { - conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) | - (hdev->esco_type & EDR_ESCO_MASK); + if (conn->out && !conn->no_autoretry && conn->attempt < 2) { + conn->pkt_type = hdev->esco_type & SCO_ESCO_MASK; hci_setup_sync(conn, conn->link->handle); goto unlock; } @@ -2801,7 +2800,7 @@ static inline void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &ev->bdaddr); if (!conn) { - conn = hci_conn_add(hdev, LE_LINK, 0, &ev->bdaddr); + conn = hci_conn_add(hdev, LE_LINK, &ev->bdaddr); if (!conn) { BT_ERR("No memory for new connection"); hci_dev_unlock(hdev); diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index ed602042a95..042a41ded46 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -1108,11 +1108,11 @@ int l2cap_chan_connect(struct l2cap_chan *chan) auth_type = l2cap_get_auth_type(chan); if (chan->dcid == L2CAP_CID_LE_DATA) - hcon = hci_connect(hdev, LE_LINK, 0, dst, - chan->sec_level, auth_type); + hcon = hci_connect(hdev, LE_LINK, dst, + chan->sec_level, auth_type, NULL); else - hcon = hci_connect(hdev, ACL_LINK, 0, dst, - chan->sec_level, auth_type); + hcon = hci_connect(hdev, ACL_LINK, dst, + chan->sec_level, auth_type, NULL); if (IS_ERR(hcon)) { err = PTR_ERR(hcon); diff --git a/net/bluetooth/lib.c b/net/bluetooth/lib.c index 86a6bed229d..f9b2dde4c0a 100644 --- a/net/bluetooth/lib.c +++ b/net/bluetooth/lib.c @@ -136,6 +136,8 @@ int bt_to_errno(__u16 code) return EPROTONOSUPPORT; case 0x1b: + case 0x1c: + case 0x1d: return ECONNREFUSED; case 0x19: diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c index 98327213d93..9d8a5bd20a6 100644 --- a/net/bluetooth/mgmt.c +++ b/net/bluetooth/mgmt.c @@ -1372,7 +1372,7 @@ static int pair_device(struct sock *sk, u16 index, unsigned char *data, u16 len) auth_type = HCI_AT_DEDICATED_BONDING_MITM; } - conn = hci_connect(hdev, ACL_LINK, 0, &cp->bdaddr, sec_level, auth_type); + conn = hci_connect(hdev, ACL_LINK, &cp->bdaddr, sec_level, auth_type, NULL); if (IS_ERR(conn)) { err = PTR_ERR(conn); goto unlock; diff --git a/net/bluetooth/sco.c b/net/bluetooth/sco.c index febc83a5c22..3b52e4729be 100644 --- a/net/bluetooth/sco.c +++ b/net/bluetooth/sco.c @@ -177,7 +177,7 @@ static int sco_connect(struct sock *sk) { bdaddr_t *src = &bt_sk(sk)->src; bdaddr_t *dst = &bt_sk(sk)->dst; - __u16 pkt_type = sco_pi(sk)->pkt_type; + struct bt_sco_parameters *param = &sco_pi(sk)->param; struct sco_conn *conn; struct hci_conn *hcon; struct hci_dev *hdev; @@ -193,17 +193,18 @@ static int sco_connect(struct sock *sk) if (lmp_esco_capable(hdev) && !disable_esco) type = ESCO_LINK; - else { + else type = SCO_LINK; - pkt_type &= SCO_ESCO_MASK; - } - hcon = hci_connect(hdev, type, pkt_type, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING); + hcon = hci_connect(hdev, type, dst, + BT_SECURITY_LOW, HCI_AT_NO_BONDING, param); if (IS_ERR(hcon)) { err = PTR_ERR(hcon); goto done; } + hcon->no_autoretry = sco_pi(sk)->no_autoretry; + conn = sco_conn_add(hcon, 0); if (!conn) { hci_conn_put(hcon); @@ -404,10 +405,24 @@ static void sco_sock_close(struct sock *sk) static void sco_sock_init(struct sock *sk, struct sock *parent) { + struct sco_pinfo *pi = sco_pi(sk); + BT_DBG("sk %p", sk); if (parent) sk->sk_type = parent->sk_type; + + pi->param.tx_bandwidth = 8000; + pi->param.rx_bandwidth = 8000; + pi->param.max_latency = HCI_SYNC_MAX_LATENCY_DONTCARE; + + /* Only Air Coding Format matters here, other data will be + * overriden by device settings during connection setup. + */ + pi->param.voice_setting = HCI_SYNC_AIR_CODING_CVSD; + + pi->param.retrans_effort = HCI_SYNC_RETRANS_EFFORT_DONTCARE; + pi->param.pkt_type = ALL_ESCO_MASK; } static struct proto sco_proto = { @@ -493,7 +508,6 @@ static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int alen) } else { /* Save source address */ bacpy(&bt_sk(sk)->src, &sa.sco_bdaddr); - sco_pi(sk)->pkt_type = sa.sco_pkt_type; sk->sk_state = BT_BOUND; } @@ -533,7 +547,6 @@ static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen /* Set destination address and psm */ bacpy(&bt_sk(sk)->dst, &sa.sco_bdaddr); - sco_pi(sk)->pkt_type = sa.sco_pkt_type; err = sco_connect(sk); if (err) @@ -640,7 +653,6 @@ static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst); else bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src); - sa->sco_pkt_type = sco_pi(sk)->pkt_type; return 0; } @@ -674,13 +686,45 @@ static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock, static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen) { struct sock *sk = sock->sk; + int len; int err = 0; + struct bt_sco_parameters *param; + u32 opt; BT_DBG("sk %p", sk); + if (level != SOL_BLUETOOTH) + return -ENOPROTOOPT; + lock_sock(sk); switch (optname) { + case BT_SCO_PARAMETERS: + /* We do not support changing SCO parameters during + * connection. + */ + if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) { + err = -EBUSY; + break; + } + + param = &sco_pi(sk)->param; + + len = min_t(unsigned int, sizeof(*param), optlen); + if (copy_from_user((char *) param, optval, len)) + err = -EFAULT; + + break; + + case BT_NO_AUTORETRY: + if (get_user(opt, (u32 __user *) optval)) { + err = -EFAULT; + break; + } + + sco_pi(sk)->no_autoretry = opt; + break; + default: err = -ENOPROTOOPT; break; @@ -750,18 +794,36 @@ static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char { struct sock *sk = sock->sk; int len, err = 0; + struct bt_sco_parameters *params; BT_DBG("sk %p", sk); if (level == SOL_SCO) return sco_sock_getsockopt_old(sock, optname, optval, optlen); + if (level != SOL_BLUETOOTH) + return -ENOPROTOOPT; + if (get_user(len, optlen)) return -EFAULT; lock_sock(sk); switch (optname) { + case BT_SCO_PARAMETERS: + params = &sco_pi(sk)->param; + + len = min_t(unsigned int, len, sizeof(*params)); + if (copy_to_user(optval, (char *) params, len)) + err = -EFAULT; + + break; + + case BT_NO_AUTORETRY: + if (put_user(sco_pi(sk)->no_autoretry, (u32 __user *) optval)) + err = -EFAULT; + break; + default: err = -ENOPROTOOPT; break; -- cgit v1.2.3